0

私はフォーム認証を持っています:

<authentication mode="Forms">
</authentication>
<authorization>
  <deny users="?" />
</authorization>

ログインページがあり、資格情報を確認するために次のことを行います。

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;
    }
}

これは機能します。次に、メンバーグループを確認したいと思います。したがって、私は:

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;
}

ローカルホストでは機能しますが、公開すると機能せず、次のエラーが発生します。

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

追加してみました

using (System.Web.Hosting.HostingEnvironment.Impersonate())

そしてまた

<identity impersonate="true"/>

次に、プールの ID をNetworkServiceandに変更しようとしましLocalSystemたが、何も変わりませんでした。自分の PC でApplicationPoolIdentity.. を使用してもすべてが正常に機能する理由がわかりません。

4

1 に答える 1

0

にユーザー名とパスワードを追加する必要がありましたPrincipalContext

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, domain, username, password);
于 2015-12-22T11:50:26.213 に答える