繁体   English   中英

具有使用 SSL 的上下文的 UserPrincipal

[英]UserPrincipal with context that uses SSL

我有一个使用 SSL 的 PrincipalContext。 这在使用 Context.ValidateCredentials() 之类的方法时效果很好。 但是,当我需要使用 UserPrincipal.FindByIdentity() 查找用户时,出现以下错误:

System.Runtime.InteropServices.COMException:服务器不愿意处理请求。 在 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 在 System.DirectoryServices.DirectoryEntry.Bind() .AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase,布尔ownCtxBase,字符串用户名,字符串密码,ContextOptions选项)在System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry条目)在System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() - -- 内部异常堆栈跟踪结束 --- 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInit() 在 System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit() 在 System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() 在 System. DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() 在 Sy stem.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) 在 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue) 在 System. DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext 上下文,String identityValue)

我的方法:

public List<string> GetUserInfo(string user) {
        var list = new List<string>();

        using (var context = new PrincipalContext(ContextType.Domain, "xxxx.xxxx.xxxx:636", "DC=xxxx,DC=xxxx,DC=xxxx", ContextOptions.SimpleBind | ContextOptions.Sealing | ContextOptions.SecureSocketLayer)) {
            var uP = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, user);

            //Do stuff with uP
        return list;
    }

但这工作正常:

public bool ValidateCredentials(string username, string password) {
        using (var context = new PrincipalContext(ContextType.Domain, "xxxx.xxxx.xxxx:636", "DC=xxxx,DC=xxxx,DC=xxxx", ContextOptions.SimpleBind | ContextOptions.Sealing | ContextOptions.SecureSocketLayer)) {
            return context.ValidateCredentials(username, password);
        }
    }

为什么我不能使用带 SSL 的上下文来处理 UserPrincipal? 如果我删除 SSL 它工作正常..

我将ContextOptions更改为Negotiate和SSL。 然后它起作用了

不幸的是,没有足够的代码示例展示如何配置 PrincipalContext 或 DirectoryEntry 以使用 LDAPS(SSL Active Directory)。 我为这个问题找到了这些解决方案:

配置 PrincipalContext 以使用 LDAPS:

var path = "test.domainName.local:636";
ContextOptions options = ContextOptions.Negotiate | ContextOptions.SecureSocketLayer;
using (var context = new PrincipalContext(ContextType.Domain, path, "DC=xyz,DC=local", options))
{
 pr("Name: " + context.Name);
 pr("ConnectedServer: " + context.ConnectedServer);
 pr("Container: " + context.Container);
 pr("UserName: " + context.UserName);
}

配置 DirectoryEntry 以使用 LDAPS:

string path = "LDAP://test.domainName.local:636";
var dic = new DirectoryEntry(path);
pr("Name: " + dic.Name);
pr("Path: " + dic.Path);
pr("AuthenticationType: " + dic.AuthenticationType);
pr("SchemaClassName: " + dic.SchemaClassName);
pr("Username: " + dic.Username);

MSDN 文档中,密封选项“只能与协商上下文选项一起使用,不能与简单绑定选项一起使用。” 这似乎没有给您带来任何错误,但您应该注意一些事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM