UserPrincipal.FindByIdentity引发异常

编程入门 行业动态 更新时间:2024-10-25 23:22:57
本文介绍了UserPrincipal.FindByIdentity引发异常-服务器上没有此类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在一个简单的场景中苦苦挣扎:我想使用用于登录计算机的用户名和密码从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引发异常

本文发布于:2023-06-06 02:23:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/534735.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   UserPrincipal   FindByIdentity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!