Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top