繁体   English   中英

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

[英]MVC 5 Redirect to Login Page Not Working with OWIN

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

我在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”

<system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
</system.webServer>

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

<system.webServer>
    <modules>
     <remove name="FormsAuthenticationModule" />
     <remove name="FormsAuthentication" />
    </modules>
</system.webServer>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM