4

I want to get the username of the logon username and domain. My code for getting it, is in a controller:

User.Identity.GetUserId()

In my web.config is:

<system.web>
    <identity impersonate="false"/>
    <authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime />
    <pages controlRenderingCompatibilityVersion="4.0" />
</system.web>

Currently, I only get a empty string. What I have to change to get the Windows username information?

Sidenote for others: While my research, I also got to the following:

Environment.UserDomainName +  @"\" + Environment.UserName

In reference to this, it only delivers the identity in which the thread is running and not the windows information.

EDIT1: I'm currently testing my program in debugging mode.

yesfabime
  • 814
  • 1
  • 12
  • 27

2 Answers2

3

try this

HttpContext.User.Identity.Name

also make sure you have authorization tag in your system.web in web.config as

<authorization>
  <allow users="?" />
</authorization>

and in IIS make sure that you will disable annonymous authentication and enable windows for the same app as

enter image description here

Prashant Mohite
  • 758
  • 2
  • 7
  • 19
  • also returns an empty string – yesfabime Sep 13 '16 at 14:54
  • This get me another error: "HTTP Error 404.15 - The request filtering module is configured to deny a request where the query string is too long." – yesfabime Sep 13 '16 at 15:12
  • The authorization setting is just to deny the anonymous user. it should not cause any kind of issue.Please see this link http://stackoverflow.com/questions/11636386/how-to-configure-the-web-config-to-allow-requests-of-any-length Also make sure you don't have [AllowAnonymous] attribute on any action http://stackoverflow.com/questions/28483745/http-error-404-15-not-found-because-the-query-string-is-too-long – Prashant Mohite Sep 13 '16 at 15:20
0

User.Identity.GetUserId() is an extension method from Microsoft.AspNet.Identity. Identity is incompatible with Windows Auth (you have to use one or the other), so I'm not sure what you're actually doing here.

Generally speaking, in Windows Auth, the value of User.Identity.Name will be in the form of {DOMAIN}\{USER}. So if you wanted the domain and/or username, you can split that string on \ and take the part you're after.

When it comes to Identity, User.Identity.Name should be the value of ApplicationUser.UserName, but depending on the setup, it might be ApplicationUser.Email.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444