문제

Using .Net 4.0 The line of code to create the PrincipalContext is:

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. It was something like user.logondomain but I can't find it any more. It was not striping the domain off the user.identity.name.

This is using windows authentication in an ASP.NET web app.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top