0

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.

murisonc
  • 135
  • 1
  • 8

1 Answers1

1

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.

Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • Nope. I'm aware of that method. What I'm looking for is some variable that already exists. – murisonc Dec 18 '12 at 20:43
  • That is exactly what I was looking for. And it returns the proper name here on my dev box but I'm guessing that is because it's dev? VS2010 web app running with windows authentication using IIS Express? – murisonc Dec 18 '12 at 21:32
  • It returns your domain name because you are the one who executes the code. If your application will run in the production environment, it will show the domain of the account under which the code is executed, not the domain of the user which makes the current request. – Alex Filipovici Dec 18 '12 at 21:42