ca9163d9
ca9163d9

Reputation: 29227

User.Identity.Name is null?

I used visual studio 2015 created a simple web api project on my office PC which Windows logged in company Domain(AD). I changed web.config file for integration authentication - <system.web><authentication mode="Windows" />. The url http://localhost:23151/api/Model1 returns a valid xml data from database.

I changed Anonymous Authentication to Disabled and Windows Authentication to Enabled in the project property for IIS Express (Development Server) settings.

I set a break point in public IQuerybal<Model1> GetModel1() and check the value of User.Identity.Name and it returns null?

Updates: What's strange is I created a new project on another PC (still in Visual studio 2015) and I can got my Windows login from property User.Identity.Name.

Update 2: I tried to copy the project files to the new PC and it still got the same issue. So it should be some settings in the project. Very strange.

Upvotes: 5

Views: 11419

Answers (2)

Froggy
Froggy

Reputation: 359

I also had the issue that User.Identity.Name is null, my environment was AspNetCore MVC. It turned out that on some configuration change, Visual Studio changed my web.config and modified forwardWindowsAuthToken to "false". It needed to be true to populate User.Identity.Name:

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="true" />

Upvotes: 0

Chris Pratt
Chris Pratt

Reputation: 239440

There's not a lot to work with in your question, but my best guess is that you've neglected to authorize the controller action. You need to add the [Authorize] attribute either to the actual controller class (to protect all actions within) or to the individual action you're working with here.

If you need to allow anonymous access to the same action(s), you can also add the [AllowAnonymous] attribute, but if you don't at least have [Authorize], then none of the user information is populated.

Upvotes: 8

Related Questions