C# MVC4应用程序在运行时首先使用Windows身份验证进入登录页面

C# MVC4应用程序在运行时首先使用Windows身份验证进入登录页面,c#,asp.net-mvc-4,windows-authentication,iis-express,C#,Asp.net Mvc 4,Windows Authentication,Iis Express,我目前正在开发一个项目,该项目将使用Windows身份验证来授权用户并设置其特定权限。但是,每次我运行程序进行测试时,在我创建项目/帐户/登录时,VisualStudio创建的默认登录页总是首先出现,而不是转到主页/索引。URL通常是http://localhost:50848/Account/Login?ReturnUrl=%2fViews%2fHome%2fIndex.cshtml 当我启动它时,而不仅仅是http://localhost:50848. 我还应该注意,程序在屏幕上获得了正确

我目前正在开发一个项目,该项目将使用Windows身份验证来授权用户并设置其特定权限。但是,每次我运行程序进行测试时,在我创建项目/帐户/登录时,VisualStudio创建的默认登录页总是首先出现,而不是转到主页/索引。URL通常是http://localhost:50848/Account/Login?ReturnUrl=%2fViews%2fHome%2fIndex.cshtml 当我启动它时,而不仅仅是http://localhost:50848. 我还应该注意,程序在屏幕上获得了正确的Windows身份验证,因此我知道该部分正在工作

在我的设置中是否有什么东西可以更改以阻止这个额外的登录屏幕弹出?我正在使用Visual Studio 2012、IIS Express和MVC 4。相关代码:

Web.config

    <appSettings>
        <add key="webpages:Version" value="2.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="PreserveLoginUrl" 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.5" />
    <httpRuntime targetFramework="4.5" />
    <!--<authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>-->
      <authentication mode="Windows"/>
      <authorization>
          <deny users="?"/>
      </authorization>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>

<system.webServer>
    <validation validateIntegratedModeConfiguration="true" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
IIS Express applicationhost.config

   <authentication>

            <anonymousAuthentication enabled="false" userName="" />

            <basicAuthentication enabled="false" />

            <clientCertificateMappingAuthentication enabled="false" />

            <digestAuthentication enabled="false" />

            <iisClientCertificateMappingAuthentication enabled="false">
            </iisClientCertificateMappingAuthentication>

            <windowsAuthentication enabled="true">
                <providers>
                    <add value="Negotiate" />
                    <add value="NTLM" />
                </providers>
            </windowsAuthentication>

        </authentication>

是的,我已将Windows身份验证更改为“已启用”,将匿名身份验证更改为“已禁用”。

控制器中是否有其他控件指定用户在访问任何页面之前必须登录

范例

[Authorize(Roles = "admin")] // this can even be declared at the top of the controller and the controllers will force all to login.
public ActionResult TheController()
{
  //TODO
}

如果您不打算使用基于cookie的身份验证,我建议您删除FormsAuthentication模块

<system.webServer>
  <modules>
    <remove name="FormsAuthenticationModule" />
  </modules>
</system.webServer>
然后它将不再重定向到默认值


此外,如果您没有使用SimpleMembership,请继续从您的帐户控制器中删除所有成员代码,并通过nuget卸载与之相关的各种软件包。这将包括WebMatrix、oauth和openid包。

事实证明,在创建MVC ASP.NET应用程序时,会生成两个web.config文件。我的更改进入了错误的web.config文件——视图文件夹下的文件,而不是应用程序中的主文件。通过将我原始问题中的更改应用到另一个配置文件,应用程序可以按预期工作


有关这两个web.config文件的更多信息,请参阅此SO问题:

您可以在StartUp.cs中对配置AppBuilder的代码进行注释

  public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            //ConfigureAuth(app);
        }
    }

不,没有。我只是添加了一个来快速测试它,但没有任何更改。您没有system.webServer部分吗?另外,您的帐户/登录方法中是否有[AllowAnonymous]。@ErikFunkenbusch我有,我刚刚更新了我的代码。还有其他我丢失的东西吗?