使用Windows身份validation时,MVC5重定向到Login.aspx

从MVC 4升级到MVC 5后,我的应用程序(从Visual Studio中启动时)会出现以下错误。 值得注意的是,我在同一个项目中托管MVC5和WebAPI2项目,因为可能存在干扰。 我还安装了dotnetopenauth nuget包(我已删除):

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /login.aspx 

我不确定为什么这是因为filter或控制器上没有设置授权属性。

Global.asax中:

  public class MvcApplication : HttpApplication { protected void Application_Start() { IDependencyInjectionBuilder dependencyInjectionBuilder = new DependencyInjectorBuilder(); var builder = new ContainerBuilder(); builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); //For WebAPI dependency injection var injector = new AutofacDependencyInjector(); dependencyInjectionBuilder.RegisterTypesAndInstances(injector, false, true); DependencyInjectionRegistration.RegisterServices(injector); builder.Update(injector.Container.ComponentRegistry); var autofacDependencyResolver = new AutofacDependencyResolver(injector.Container); DependencyResolver.SetResolver(autofacDependencyResolver); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(injector.Container); //For WebAPI dependency injection AreaRegistration.RegisterAllAreas(); AutoMapperConfig.RegisterMappings(Mapper.Configuration); //Reference: http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2 GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); GlobalConfiguration.Configuration.EnsureInitialized(); #if DEBUG // Uncomment this line for the nhibernate profiler // and add HibernatingRhinos.Profiler.Appender.dll // to the PD.UserInterfacePbj project references //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); #endif } } 

Root Web.config:

    
NHibernate.Connection.DriverConnectionProvider NHibernate.Dialect.MsSql2008Dialect NHibernate.Driver.SqlClientDriver ... <!---->

查看Web.config:

     

我可以通过根据对此问题的回复将以下内容添加到appSettings部分来解决此问题 。

   

此外,Web.Config System.web部分不应包含此行:

  

在我看来它很有效。

如果您在本地运行应用程序并收到http 401错误,则可能需要更改IISExpress配置。 转到Documents> IISExpress> applicationhost.config

确保启用了WindowsAuthentication:

        

除了上述答案之外,根据用于创建项目的模板,您可以在App_Start文件夹中具有Startup.Auth.cs,其中包含以下可注释掉的代码:

 // Enable the application to use a cookie to store information for the signed in user app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login") }); // Use a cookie to temporarily store information about a user logging in with a third party login provider app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);