简体   繁体   English

如何获取用户域以创建主体上下文asp.net

[英]How to get the users domain to create a principalcontext asp.net

Using .Net 4.0 The line of code to create the PrincipalContext is: 使用.Net 4.0创建PrincipalContext的代码行是:

PrincipalContext context = new PrincipalContext(ContextType.Domain, "domain");

A while back I saw a code snippet where you don't have to specify the domain name but instead use a system or httpcontext variable to pass in the domain name. 不久前,我看到了一个代码片段,您无需指定域名,而是使用系统变量或httpcontext变量来传递域名。 It was something like user.logondomain but I can't find it any more. 这有点像user.logondomain,但我找不到了。 It was not striping the domain off the user.identity.name. 它没有从user.identity.name剥离域。

This is using windows authentication in an ASP.NET web app. 这是在ASP.NET Web应用程序中使用Windows身份验证。

This should work: 这应该工作:

string domain = "defaultDomain";
string[] splittedAuth = Request.ServerVariables["LOGON_USER"].Split('\\');
domain = (splittedAuth.Count() > 1) ? splittedAuth[0] : domain;
PrincipalContext context = new PrincipalContext(ContextType.Domain, domain);

If you were referring to the Environment.UserDomainName Property , this is definitely not what you need, as it will return the domain of the account under which the code is executed, which is not the scenario for ASP.NET. 如果您引用的是Environment.UserDomainName属性 ,则绝对不是您所需要的,因为它将返回执行代码所在帐户的域,而ASP.NET则不是这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM