trying to get the data from active directory

Anjali Agarwal
656
Reputation points
Hello,
I am trying to get the data from active directory. this is my code:
UserPrincipal up = UserADIdentity.GetADUserInfo(WindowsIdentity.GetCurrent().Name.ToString());
public static UserPrincipal GetADUserInfo(string userName)
{
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
UserPrincipal userPrincipal = new UserPrincipal(ctx);
userPrincipal.SamAccountName = userName;
userPrincipal.Enabled = true;
userPrincipal = new PrincipalSearcher(userPrincipal).FindOne() as UserPrincipal;
}
}
Code works fine on my local computer, but when I deploy the code to IIS on server. This line of code:
WindowsIdentity.GetCurrent().Name.ToString()
is always returning NULL. I am not sure how to fix it. My code is running under applicationPoolIdentity. This is what i have in my startup.cs class:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication= true;
});
services.AddAuthentication(IISDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Home/Index";
});
}
{count} votes
I am using ASP.net core. I want to get the current log in user for windows authentication. yes I enabled windows authentication at IIS server. below is my configureService in startup.cs file:
I suggest you could try
HttpContext.User.Identity.Name;
and make sure you have disable the anonymous authentication.