首页 > 解决方案 > 不断提示输入凭据

问题描述

到目前为止我做了什么:

  1. 使用此 web.config 创建网站(这只是设置部分,而不是整个文件 :))

    <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    <add key="autoFormsAuthentication" value="false"/>
    <add key="enableSimpleMembership" value="false"/>
    </appSettings>
    <system.web>
    <compilation debug="true" targetFramework="4.6.2"/>
    <httpRuntime targetFramework="4.6.2"/>
    <authentication mode="Windows"></authentication>
    <authorization>
      <deny users="?"/>
    </authorization>
    </system.web>
    
  2. 创建了一个控制器:

    [Authorize(Users = @"myPcName\myUserName,skynet\Simple")]
    public class AuthenController : Controller
    {
        [Authorize(Users = @"myPcName\myUserName")]
        public ActionResult ForAdministrator()
        {
            return View();
        }
        [Authorize(Users = @"myPcName\Simple")]
        public ActionResult ForUser()
        {
            return View();
        }
    }
    

我通过以下方式获得了我的凭据:cmd -> whoami

  1. 我以发布模式将我的 mvc 站点发布到 c:\inetpub\wwwroot\backoffice
  2. 在 IIS 中: 在此处输入图像描述

  3. 我什至在 Intranet 选项中将我的网站添加到 Local Intranet,并且: 在此处输入图像描述

它只是一次又一次地提示我输入凭据:

在此处输入图像描述

标签: c#asp.net-mvcauthenticationwindows-authentication

解决方案


只是为了帮助遇到这个问题的任何人:我解决问题的方法是:单击 VS 中的项目。按 F4 转到属性 将“匿名身份验证”设置为禁用

在 web.config 中:

<authentication mode="Windows"></authentication>
<authorization>
  <allow users="*" />
</authorization>

在你的控制器中:

    [Authorize(Users = @"pcName\user")]
    public ActionResult ForAdministrator()
    {
        return View();
    }

    // Authorization with windows authentication (user)
    [Authorize(Users = @"pcName\user")]
    public ActionResult ForUser()
    {
        return View();
    }

其中 pcName 是您的电脑的名称(不是 WORKGROUP!)

通过这种方式,您可以控制谁被允许(因为匿名身份验证被禁用)。

希望它可以帮助某人。罗腾


推荐阅读