i have a form authentication:
<authentication mode="Forms">
</authentication>
<authorization>
<deny users="?" />
</authorization>
I have a page with my login and to check the credential I do:
public static bool ValidateCredentials(string sUserName, string sPassword, string sDomain)
{
PrincipalContext oPrincipalContext = GetPrincipalContext(sDomain);
try
{
return oPrincipalContext.ValidateCredentials(sUserName, sPassword);
}
catch (Exception ex)
{
ASTools.LogError(ex.ToString());
return false;
}
}
this works. Then I want to check the members group. Therefore i do:
public static bool IsGroupMember(string username, string sDomain)
{
List<string> groupList = new List<string>() {"WebDeveloper", "SyncDeveloper"};
using (System.Web.Hosting.HostingEnvironment.Impersonate())
{
try
{
PrincipalContext domain = new PrincipalContext(ContextType.Domain, sDomain);
UserPrincipal user = UserPrincipal.FindByIdentity(domain, IdentityType.SamAccountName, username);
foreach (string groupname in groupList)
{
var group = GroupPrincipal.FindByIdentity(domain, groupname);
if (group != null)
{
if (group.GetMembers(true).Any(member => user.SamAccountName.ToLower() == member.SamAccountName.ToLower()))
{
return true;
}
}
}
}
catch (Exception ex)
{
ASTools.LogError(ex.ToString());
}
}
return false;
}
In localhost it works, but when I publish it doesnt and this error occurs:
System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operations error occurred.
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.PropertyValueCollection.PopulateList()
at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
at ASActiveDirectory.IsGroupMember(String username, String sDomain) in c:\inetpub\Applicazioni\DeviceManage\App_Code\ASActiveDirectory.cs:line 78
I tried to add
using (System.Web.Hosting.HostingEnvironment.Impersonate())
and also
<identity impersonate="true"/>
then i tried to change the identity of my pool to NetworkService
and LocalSystem
but nothing changed. I don't understand why in my own pc everything works fine even with ApplicationPoolIdentity
..
I know this question has been asked many times but I tried every suggestion I found and they don't work for me..
Please help me!
I had to add username and password in the PrincipalContext
:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, username, password);
User contributions licensed under CC BY-SA 3.0