Quyen T Ho
Quyen T Ho

Reputation: 55

User.Identity.Name is empty when using Window authentication

I have a website in MVC 4 with .net 4.5 running on IIS7 for example: www.mysite.com and a folder www.mysite.com/admin

the www.mysite.com is using form authentication and allow anonymous for certain pages, I would like to have www.mysite.com/admin using window authentication.

I have the setting in IIS security for that folder to use Window Authentication ( it is not an application in IIS )

In my admin Controller I need to capture the username from the domain and it is always empty the code is as followed:

   WindowsIdentity winId = WindowsIdentity.GetCurrent();         
  if (winId != null)
  {
   string name = winId.Name;        
  }

the User.Identity.Name is also empty Please let me know what am I missing.

I am sure that this is something pretty common and could not figure out why it does not work with MVC, i have the old site using web form and have the same setting and it is working.

Thanks In advance

Upvotes: 3

Views: 4281

Answers (1)

vikingben
vikingben

Reputation: 1652

If you are using windows authentication for the page try the following and see if it has any data. It sounds like you may have tried this but I didn't see the Page object included.

Make sure your web.config has windows auth enabled.

<authentication mode="Windows">
     </authentication>

string user = Page.User.Identity.Name;

Hope this helps I would probably look into the built in roles and membership features I'm not familiar with the route you are taking my apologies.

http://www.codeproject.com/Articles/342061/Understanding-ASP-NET-Roles-and-Membership-A-Begin

Upvotes: 3

Related Questions