简体   繁体   English

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

[英]Windows authentication with ASP.Net MVC 4 with VS 2012 and above

Writing this as a question and answering it self, because there were two steps i followed to resolve the issue, which was not available together. 将其写为问题并自行回答,因为我遵循了两个步骤来解决该问题,但这两个步骤无法一起使用。

Was developing an application in ASP.Net MVC 4 [internet app using razor view] using Visual Studio 2012. 正在使用Visual Studio 2012在ASP.Net MVC 4 [使用剃刀视图的互联网应用]中开发应用程序。

The default web.config entries for <authentication> was as follows: <authentication>的默认web.config条目如下:

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

I needed windows authentication for the application. 我需要该应用程序的Windows身份验证。 Hence, changed the mode to Windows 因此,将模式更改为Windows

<authentication mode="Windows" />

With the above settings, HttpContext.User.Identity.IsAuthenticated was false HttpContext.User.Identity.Name was empty string. 使用上述设置, HttpContext.User.Identity.IsAuthenticatedfalse HttpContext.User.Identity.Name为空字符串。

Select the web project and press F4.

Make the following changes: 进行以下更改:

Anonymous Authentication : Disabled, 
Windows Authentication : Enabled .

If the application is tried to run with the above changes, you will get HTTP 404 error and application will be redirected to domainname/login.aspx?ReturnUrl=%2f. 如果尝试使用上述更改运行该应用程序,则会出现HTTP 404错误,并且该应用程序将被重定向到domainname / login.aspx?ReturnUrl =%2f。

Along with above changes there are two more keys which is to be added in the <appSettings> section in the web.config file. 除了上述更改外,还需要在web.config文件的<appSettings>部分中添加另外两个键。

<add key="autoFormsAuthentication" value="false" />
<add key="enablesimplemembership" value="false"/>

Now if you run the application you will be an authenticated user and also get the name. 现在,如果您运行该应用程序,您将成为经过身份验证的用户,并且还会获得名称。

HttpContext.User.Identity.IsAuthenticated will be true

and

HttpContext.User.Identity.Name will have the user name.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM