Reputation: 22054
I am working on a hosted ASP.Net application, with Windows Authentication, and I need to get the username for various personalisation tasks. The directory on the host machine is set to have Windows Authentication and the web.config
file also has:-
<authentication mode="Windows">
</authentication>
in it. I have tried various methods within the application to get the user name with the following results:-
Source Result
System.Web.HttpContext.Current.Request.LogonUserIdentity.Name 'EZ\urlname_a'
System.Environment.UserName 'NETWORK SERVICE'
System.Web.HttpContext.Current.User.Identity.Name ''
this.Request.ServerVariables["LOGON_USER"] ''
this.Request.ServerVariables["AUTH_USER"] ''
this.User.Identity.Name ''
thanks to the answers here, here, and here, among others. None of these distinguish between the various usernames that were entered.
Is there anything else I can try in code to get the name (or some proxy for the name)? Is there anything I can check for in web.config
? Or if I need to take the matter up with the host, is there any intelligent question I can ask?
Edit I have set the application directory and .Net version using the host's IIS Dialogue.
Upvotes: 2
Views: 182
Reputation: 355
You need to set IIS to use Windows Authentication only, then use Page.User.Identity.Name or similar to fetch the windows user name.
From the Authentication pane, set the Windows Authentication to Enabled and Disable anonymous authentication.
Upvotes: 1