使用VS. 2012及更高版本的ASP.Net MVC 4进行Windows身份验证

Rav*_*Rao 0 c# asp.net-mvc windows-authentication asp.net-mvc-4

把它写成一个问题并自己回答,因为我有两个步骤来解决这个问题,而这个问题并不是一起提供的.

正在使用Visual Studio 2012在ASP.Net MVC 4 [使用剃刀视图的互联网应用程序]中开发应用程序.

默认的web.config条目<authentication>如下:

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

我需要为应用程序进行Windows身份验证.因此,将模式更改为Windows

<authentication mode="Windows" />
Run Code Online (Sandbox Code Playgroud)

用上面的设置,HttpContext.User.Identity.IsAuthenticated当时false HttpContext.User.Identity.Name是空字符串.

Rav*_*Rao 5

Select the web project and press F4.
Run Code Online (Sandbox Code Playgroud)

进行以下更改:

Anonymous Authentication : Disabled, 
Windows Authentication : Enabled .
Run Code Online (Sandbox Code Playgroud)

如果尝试使用上述更改运行应用程序,您将收到HTTP 404错误,应用程序将重定向到domainname/login.aspx?ReturnUrl =%2f.

除了上述更改之外,还有两个键要添加<appSettings>到web.config文件的部分中.

<add key="autoFormsAuthentication" value="false" />
<add key="enablesimplemembership" value="false"/>
Run Code Online (Sandbox Code Playgroud)

现在,如果您运行该应用程序,您将成为经过身份验证的用户并获得该名称.

HttpContext.User.Identity.IsAuthenticated will be true
Run Code Online (Sandbox Code Playgroud)

HttpContext.User.Identity.Name will have the user name.
Run Code Online (Sandbox Code Playgroud)