【问题标题】:Windows authentication with ASP.Net MVC 4 with VS 2012 and above带有 VS 2012 及更高版本的 ASP.Net MVC 4 的 Windows 身份验证
【发布时间】:2014-03-28 08:33:56
【问题描述】:

将其写为问题并自行回答,因为我遵循了两个步骤来解决问题,而这两个步骤不能一起使用。

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

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

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

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

<authentication mode="Windows" />

通过上述设置,HttpContext.User.Identity.IsAuthenticatedfalse HttpContext.User.Identity.Name 为空字符串。

【问题讨论】:

  • 好的,您是否在网络服务器上设置了 NTLM 身份验证?
  • @DavidHirst :已发布步骤作为使其在本地运行的答案。未在网络服务器上设置 NTLM 身份验证。需要以下过程才能使应用程序在本地以调试模式运行。

标签: c# asp.net-mvc asp.net-mvc-4 windows-authentication


【解决方案1】:
Select the web project and press F4.

进行以下更改:

Anonymous Authentication : Disabled, 
Windows Authentication : Enabled .

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

除了上述更改之外,还有两个键要添加到 web.config 文件的 &lt;appSettings&gt; 部分。

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

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

HttpContext.User.Identity.IsAuthenticated will be true

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

【讨论】:

    猜你喜欢
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多