使用.Net 4.0 创建PrincipalContext的代码行是:
PrincipalContext context = new PrincipalContext(ContextType.Domain, "domain");
前段时间我看到了一个代码片段,您不必指定域名,而是使用系统或httpcontext变量来传递域名。它就像user.logondomain,但我再也找不到了。它没有从user.identity.name。
中删除域这是在ASP.NET Web应用程序中使用Windows身份验证。
答案 0 :(得分:1)
这应该有效:
string domain = "defaultDomain";
string[] splittedAuth = Request.ServerVariables["LOGON_USER"].Split('\\');
domain = (splittedAuth.Count() > 1) ? splittedAuth[0] : domain;
PrincipalContext context = new PrincipalContext(ContextType.Domain, domain);
如果你指的是Environment.UserDomainName Property,这肯定是不你需要什么,因为它会返回执行代码的帐户的域名,这不是ASP.NET的场景。