ASP.Net getting Active Directory username problem

travisowens

Centurion
Joined
Feb 11, 2004
Messages
108
Location
Rochester, NY
I have an Intranet ASP.Net app that retrieves the current visitors Active Directory username via
HttpContext.Current.User.Identity.Name
which works fine locally (after setting Directory Security on the folder, of course, also set on the test server) and for other users it works fine on my test server but when I view the webapp on the test server the app always returns the local admin account (ex: servername/Administrator) instead of my AD username (ex: domain/JDoe).

How do I force the AD credentials take precedence over the server local credentials?
 
1. Is the server on the same domain as your active directory server?
2. Have you made sure that the Windows Authentication box is checked in your projects IIS settings?
 
You didn't mention it so....

And you have windows authentication in the web.config file?
 
UCommonASP.cs contains...
Code:
using System;
using System.Web;

namespace UCommon
{
  public class ASP
  {
    public static string CurrentASPUserName()
    {
      string username = HttpContext.Current.User.Identity.Name;

      if (username != "" && username != null)
      {
        // this returns the format of "domain/username" so we must split it
        string[] userinfo = HttpContext.Current.User.Identity.Name.Split(Convert.ToChar("\\"));
        // the username will always be the 2nd item in the array
        return userinfo[1];
      }
      else
      {
        return "Unknown";
      }
    }
  }
}
Web.config contains

Code:
<authentication mode="Windows" />
FYI I get the same result for the username despite if I use a seperate class or if I do it within my code.

In my app I do the following
Code:
Response.Write("<!-- Class UserName='" + UCommon.ASP.CurrentASPUserName() + "' -->\n");
Response.Write("<!-- Local UserName='" + HttpContext.Current.User.Identity.Name + "' -->\n");

Which prints out the following when access this app on the server from my PC, please note it says this ONLY on my PC, if I log into any other PC I get my proper Active Directory username. Please note my CurrentASPUserName() splits the domain & username before returning it.
<!-- Class UserName='Administrator' -->
<!-- Local UserName='DEVGRPQA\Administrator' -->


When I run the app locally via VS or access it from another PC to the server I see:
<!-- Class UserName='towens' -->
<!-- Local UserName='UNITY_HS\towens' -->


The app works fine for everybody else, and my desktop IS joined to the domain and I'm logged into the domain also.
 
Back
Top