MVC 5重定向到登录页面不使用OWIN

我正试着用OWIN来解决问题。 我创建了两个MVC 5项目。 一个使用Aspnet.Identity进行身份validation,另一个使用另一个空项目。

我在emptyp项目中添加了以下内容:

  • 具有登录操作和相应视图的帐户控制器

  • Startup.cs和另一个部分Startup.cs

public partial class Startup { public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "ApplicationCookie", LoginPath = new PathString("/Account/Login") }); } } 

我在Home控制器中使用两个项目中的[Authorize]属性修饰了About操作。

当我运行第一个项目并在登录前进入“关于”屏幕时,它会按预期重定向到登录操作。 当我为第二个项目做同样的事情时,我得到一个“HTTP错误401.0 – 未经授权”而不是重定向。

知道是什么会导致第二个这样做吗?

我创建了两个新的类似项目,并能够重现您的错误。

在空白项目中,我不得不安装Microsoft.Owin.Host.SystemWeb (通过Nuget),一旦我这样做,我在Startup.cs类中遇到了一堆错误。 结束了这个:

 [assembly: OwinStartupAttribute(typeof(v2.Startup))] namespace v2 { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = "ApplicationCookie", LoginPath = new PathString("/Account/Login") }); } } } 

最后,当我调用[Authorize]属性修饰的About()方法时,我现在能够访问/查看我的Login视图。

希望这可以帮助! 文斯

每个ASP.NET MVC 5 Web.config:“FormsAuthenticationModule”或“FormsAuthentication”

      

为了额外的安全性我留下了“错字”处理程序(如果微软稍后更改它给我)