更改我的域名

发布于 2025-01-05 20:27:27 字数 1352 浏览 0 评论 0原文

我有一个函数可以返回 Intranet 应用程序的 Active Directory 中的用户名:

public string GetCurrentUsersName()
    {
        //Get the username and domain information
        string user = Environment.UserName;
        string domainName = Environment.UserDomainName;

        //Set the correct format for the AD query and filter
        string ldapQueryFormat = @"LDAP://" + domainName + ".com/DC=" + domainName + ",DC=com";
        string queryFilterFormat = @"(&(samAccountName=" + user + ")(objectCategory=person)(objectClass=user))";

        SearchResult result = null;
        using (DirectoryEntry root = new DirectoryEntry(ldapQueryFormat))
        {
            using (DirectorySearcher searcher = new DirectorySearcher(root))
            {
                searcher.Filter = queryFilterFormat;
                SearchResultCollection results = searcher.FindAll();

                result = (results.Count != 0) ? results[0] : null;
            }
        }

        //Get the email property from AD
        string name = result.Properties["displayName"][0] as string;
        return name;
    }

我最近将域从 mycompany.com 更改为 mycompany.local 。现在,每当我尝试运行此方法时都会收到错误,我应该更改某些内容吗?字符串domainName过去等于mycompany,但现在它等于myco,因为那是我使用的域名。

我收到的错误是:

System.Runtime.InteropServices.COMException:服务器无法运行。

I have a function which returns a user's name in Active Directory for an Intranet application:

public string GetCurrentUsersName()
    {
        //Get the username and domain information
        string user = Environment.UserName;
        string domainName = Environment.UserDomainName;

        //Set the correct format for the AD query and filter
        string ldapQueryFormat = @"LDAP://" + domainName + ".com/DC=" + domainName + ",DC=com";
        string queryFilterFormat = @"(&(samAccountName=" + user + ")(objectCategory=person)(objectClass=user))";

        SearchResult result = null;
        using (DirectoryEntry root = new DirectoryEntry(ldapQueryFormat))
        {
            using (DirectorySearcher searcher = new DirectorySearcher(root))
            {
                searcher.Filter = queryFilterFormat;
                SearchResultCollection results = searcher.FindAll();

                result = (results.Count != 0) ? results[0] : null;
            }
        }

        //Get the email property from AD
        string name = result.Properties["displayName"][0] as string;
        return name;
    }

I've recently changed domain from mycompany.com to mycompany.local . I now receive an error whenever I try to run this method, should I change something? string domainName used to equal mycompany, but now it is equal to myco as thats the domain name I use.

The error I receive is:

System.Runtime.InteropServices.COMException: The server is not operational.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七婞 2025-01-12 20:27:27

如果您最近将域从 mycompany.com 更改为 mycompany.local,则 AD 查询和过滤器的正确格式应如下所示:

//Set the correct format for the AD query and filter
string ldapQueryFormat = @"LDAP://" + domainName + ".local/DC=" + domainName + ",DC=local"; 

将 'Com' 替换为 'local'

If you recently changed domain from mycompany.com to mycompany.local the correct format for the AD query and filter should be like :

//Set the correct format for the AD query and filter
string ldapQueryFormat = @"LDAP://" + domainName + ".local/DC=" + domainName + ",DC=local"; 

Replacing 'Com' with 'local'

坏尐絯 2025-01-12 20:27:27

是否有理由动态构建域字符串?对于不同的用户或情况会有所不同吗?如果不是,为什么不将其定义为配置设置?您可以从 Active Directory 获取域的 LDAP 地址,然后只需在网站的 web.config 文件中设置静态设置。域名应该很少更改,以便将其作为设置比尝试动态构建它更容易。

Is there a reason to build the domain string dynamically? Will it be different for different users or situations? If not, why not define it as a configuration setting? You can get the LDAP address for your domain from Active Directory, and then you just need to set a static setting in your website's web.config file. A domain name should change so infrequently that having it as a setting is easier than trying to dynamically build it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文