ASP.Net MVC4 User.Identity.Name getting empty even though set authentication mode as 'Windows'

后端 未结 2 1223
挽巷
挽巷 2021-01-19 13:28

I am working on ASP.Net MVC-4 application. I have to implement windows authentication. I have set authentication mode as \'Windows\' in web.config file as shown below.

相关标签:
2条回答
  • 2021-01-19 13:42

    With the given information, it looks like you've configured your project correctly but haven't actually authenticated the user yet.

    First some background. There is a simplified tutorial on asp.net where, in between the lines, the following statement is mentioned:

    By default, the ASP.NET Development Web Server executes all pages in the context of the current Windows account (whatever account you used to log into Windows).

    This means that when you run your project with F5, it executes everything under your currently logged in user account. However, it isn't yet authenticated for the application and therefor your User.Identity.* is not set yet.

    In order to verify if this is the case, you should add the [Authorize] attribute on the first controller (or Action) that is called in you ASP.NET MVC project. Most likely you'll be confronted with a "HTTP Error 401.0 - Unauthorized" exception. In this case, you should enable your webserver to authenticate first. The above mentioned tutorial will help you with this.

    0 讨论(0)
  • 2021-01-19 13:44

    Windows authentication is performed by IIS to establish our managed code User.Identity. Therefore, you need to enable Windows Authentication in your IIS, and in order to force the user to authenticate before being able to access our application, you need to disable Anonymous Authentication

    enter image description here

    0 讨论(0)
提交回复
热议问题