更改我的域名
我有一个函数可以返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您最近将域从 mycompany.com 更改为 mycompany.local,则 AD 查询和过滤器的正确格式应如下所示:
将 '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 :
Replacing 'Com' with 'local'
是否有理由动态构建域字符串?对于不同的用户或情况会有所不同吗?如果不是,为什么不将其定义为配置设置?您可以从 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.