trying to get the data from active directory
Anjali Agarwal
1,531
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";
});
}
Sign in to answer