0

I am using windows authentication in a ASP.Net Web Application and in the code-behind attempting to retrieve the logged in user name. I am using this routine WindowsIdentity.GetCurrent().Name but instead of getting the name of the user, I am getting - NT AUTHORITY\NETWORK SERVICE. What do I need to change or are my users logging in a different way than expected?

JohnFx
  • 34,542
  • 18
  • 104
  • 162
Craig
  • 1,205
  • 2
  • 21
  • 54

2 Answers2

3

Use this.Context.User.Identity.Name, which will get the identity for the current HTTPContext object: http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx

James Shuttler
  • 1,344
  • 9
  • 15
  • 1
    to add to James answer, the WindowsIdentity.GetCurrent() is returning the user running the application pool, not the user logged into your app – Anthony Shaw Jan 19 '12 at 19:59
1

Add the following to your web.config:

<system.web>
    <identity impersonate="true"/>
</system.web>

This will make your web app impersonate the currently logged in user. That does have some implications. An important one is that the logged in user must have the proper rights to files, folders, databases and so on.

If you don't want that, you can use User.Identity instead of WindowsIdentity.GetCurrent().

Anders Marzi Tornblad
  • 18,896
  • 9
  • 51
  • 66
  • `impersonate` is not needed. No matter what value is given to it, `Page.User.Identity` remains the same. `WindowsIdentity.GetCurrent()` returns application pool identity. – Lex Li Jan 01 '22 at 02:29