MVC 5重定向到登录页面与OWIN不工作 [英] MVC 5 Redirect to Login Page Not Working with OWIN

查看:111
本文介绍了MVC 5重定向到登录页面与OWIN不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的头周围采用OWIN。我创建了两个MVC 5个项目。一个身份验证使用Aspnet.Identity另一个开始作为一个空项目。

I'm trying to get my head around using OWIN. I have created two MVC 5 projects. One with authentication using Aspnet.Identity and the other started as an empty project.

我添加了以下到emptyp项目:

I added the following to the emptyp project:


  • 帐户控制器与登录操作,coresponding视图

  • Account controller with a Login action and coresponding view

Startup.cs和

Startup.cs and another partial Startup.cs with

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

我已经装饰了关于在这两个项目中的[授权]属性的Home控制器的动作。

I have decorated the About action in the Home controller with the [Authorize] attribute in both projects.

当我运行的第一个项目,然后转到关于屏幕登录之前它重定向到登录行动的期望。当我的第二个项目做同样的我得到一个HTTP错误401.0 - 未经授权,而不是重定向

When I run the first project and go to the About screen before logging in it redirects to the login action as expect. When I do the same for the second project I get a "HTTP Error 401.0 - Unauthorized" instead of redirecting.

任何想法会导致第二个表现这样?

Any idea what would cause the second one to behave this way?

推荐答案

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

I’ve created two new similar projects and was able to reproduce your error.

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

In the blank project, I had to install the Microsoft.Owin.Host.SystemWeb (via Nuget) and once I did this, I was getting a bunch of errors in my Startup.cs class. Ended up with this:

[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")
            });
        }
    }
}

在年底,现在我能打到/看到我的登录视图时,我称之为装饰着 [授权] 属性关于()的方法。

In the end, I'm now capable of hitting/seeing my Login view when I call the About() method decorated with the [Authorize] attribute.

希望这有助于!
文斯

Hope this helps! Vince

这篇关于MVC 5重定向到登录页面与OWIN不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆