关于 c#:UserPrincipal SamAccountName throws DirectoryServicesCOMException unhandled “A local error has occurred” | 珊瑚贝

UserPrincipal SamAccountName throws DirectoryServicesCOMException unhandled “A local error has occured”


我正在编写一段代码,它应该根据他们的 SamAccountName 作为搜索参数在活动目录中搜索特定用户 GivenName (forename) 和 Surname,然后返回一个包含他们的名字和姓氏的字符串.

目前我写的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static string GetName(string uName)
    {
        StringBuilder builder = new StringBuilder();
        using (PrincipalContext context = new PrincipalContext(ContextType.Domain,“serverName”))
        {
            UserPrincipal user = new UserPrincipal(context);

            user.SamAccountName = uName;

            PrincipalSearcher srch = new PrincipalSearcher(user);
            srch.QueryFilter = user;
            PrincipalSearchResult<Principal> res = srch.FindAll();
            foreach (UserPrincipal u in res)
            {
                builder.Append(u.GivenName);
                builder.Append(“”);
                builder.Append(u.Surname);
            }
            return builder.ToString();
        }
    }

我在上面的代码中遇到的问题是在运行时行

1
user.SamAccountName = uName;

抛出以下错误:DirectoryServicesCOMException unhandled “A local error has occurred”

主体上下文对象创建得很好,就像用户主体对象一样,它只在执行上述行时抛出错误。更奇怪的是,这段代码似乎在几天前就可以工作了。如果有人阅读本文对我为什么会收到此错误有任何想法,我将不胜感激!

附言
我求助于这个问题,因为该死的错误信息有点太神秘而无法真正弄清楚,或者至少对我来说(发生了本地错误)真的吗?任何认为这是有用的错误消息的开发人员都是白痴。


可能的原因:

  • 计算机的域成员身份或身份验证存在问题。例如,运行代码的计算机上的时钟是否与目标域中的 DC 同步(在 5 分钟内)?
  • 用户名无效。例如,它包含无效字符。
    • 可以这么说,感谢您的新眼睛。确实是凭证无效。我意识到,当使用 PrincipalContext.ValidateCredentials 检查凭据的有效性时,它验证没有问题。问题是我实际上并没有在上面的方法中为 PrincipalContext 提供用户名和密码。我能够通过将 PrincipalContext 参数更改为: PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName, uName, pswd


    来源:https://www.codenong.com/12458304/

    微信公众号
    手机浏览(小程序)
    0
    分享到:
    没有账号? 忘记密码?