我在一个简单的场景中苦苦挣扎:我想使用用于登录计算机的用户名和密码从Active Directory中检索我的帐户。
我的第一个问题是,尝试调用UserPrincipal.FindByIdentity时,我从服务器收到了推荐。考虑到PrincipalContext.ValidateCredentials可以正常工作,我认为这有点奇怪,但是事实证明我的DC路径不正确。
我不是确定如何正确制作我的OU / DC字符串。因此,我找到了
I'm struggling with a simple scenario: I would like to retrieve my account from Active Directory using the username and password which I use to log into my computer.
My first issue was that I was receiving a referral from the server when attempting to call UserPrincipal.FindByIdentity. I thought that this was a bit weird, given the fact that PrincipalContext.ValidateCredentials was working fine, but it turns out that my DC path was incorrect.
I wasn't sure how to properly craft my OU/DC string. As such, I found this SO post which helpful provided the following bit of code:
private static string GetDomainControllerString() { string pdc; using (var context = new PrincipalContext(ContextType.Domain)) { string server = context.ConnectedServer; // "pdc.examle" string[] splitted = server.Split('.'); // { "pdc", "example", "com" } IEnumerable<string> formatted = splitted.Select(s => String.Format("DC={0}", s));// { "DC=pdc", "DC=example", "DC=com" } string joined = String.Join(",", formatted); // "DC=pdc,DC=example,DC=com" // or just in one string pdc = String.Join(",", context.ConnectedServer.Split('.').Select(s => String.Format("DC={0}", s))); } return pdc; }After using this code to properly generate my DC string, my error message changed. Now, I am receiving the error "There is no such object on the server." I suspect the issue is either with my OU or how I am calling FindByIdentity.
Here is the location of my user account which I am trying to retrieve:
And here is how I am attempting to access said user:
private static void Main(string[] args) { const string Domain = "SLO1.Foo.Bar.biz"; const string DefaultOU = "OU=Users,DC=SLO1,DC=Foo,DC=Bar,DC=biz"; const string username = @"sanderso"; const string password = "**********"; var principalContext = new PrincipalContext(ContextType.Domain, Domain, DefaultOU, ContextOptions.Negotiate, username, password); bool areCredentialsValid = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate); if (areCredentialsValid) { UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, username); } }I have also tried calling:
UserPrincipal.FindByIdentity(principalContext, IdentityType.Name, "Sean Anderson"); UserPrincipal.FindByIdentity(principalContext, "Sean Anderson");these were equally unsuccessful.
解决方案This Code should work for you Sean I work on AD for BOA currently and use this many times..
public bool UserExists(string username) { // create your domain context PrincipalContext domain = new PrincipalContext(ContextType.Domain); // find the user UserPrincipal foundUser = UserPrincipal.FindByIdentity(domain, IdentityType.Name, username); return foundUser != null; }from MSDN what each parameter is see the list below Parameters
context Type: System.DirectoryServices.AccountManagement.PrincipalContext The PrincipalContex that specifies the server or domain against which operations are performed. identityType Type: System.DirectoryServices.AccountManagement.IdentityType A IdentityType enumeration value that specifies the format of the identityValue parameter. identityValue Type: System.String The identity of the user principal. This parameter can be any format that is contained in the IdentityType enumeration. Return Value Type: System.DirectoryServices.AccountManagement.UserPrincipal A UserPrincipal object that matches the specified identity value and type, or null if no matches are found.UserPrincipal.FindByIdentity Method()
更多推荐
UserPrincipal.FindByIdentity引发异常
发布评论