我有一个使用SSL的PrincipalContext。使用Context.ValidateCredentials()等方法时,此方法可以正常工作。但是当我需要使用UserPrincipal.FindByIdentity()找到用户时,我收到以下错误:
System.Runtime.InteropServices.COMException:服务器不愿意处理请求。 在System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 在System.DirectoryServices.DirectoryEntry.Bind() 在System.DirectoryServices.DirectoryEntry.get_SchemaEntry() 在System.DirectoryServices.AccountManagement.ADStoreCtx.IsContainer(DirectoryEntry de) 在System.DirectoryServices.AccountManagement.ADStoreCtx..ctor(DirectoryEntry ctxBase,Boolean ownCtxBase,String username,String password,ContextOptions options) at System.DirectoryServices.AccountManagement.PrincipalContext.CreateContextFromDirectoryEntry(DirectoryEntry entry) 在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() at System.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 context,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);
}
}
为什么我不能使用Context with SSL来使用UserPrincipal?如果我删除SSL它工作正常..
答案 0 :(得分:1)
我将ContextOptions更改为Negotiate和SSL。然后它工作
答案 1 :(得分:0)
遗憾的是,没有足够的代码示例展示如何配置 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);