修改CookieAuthenticationOptions LoginPath OnRedirectToReturnUrl事件 [英] Modify CookieAuthenticationOptions LoginPath OnRedirectToReturnUrl Event

查看:562
本文介绍了修改CookieAuthenticationOptions LoginPath OnRedirectToReturnUrl事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 6 ASP.NET 5项目中具有以下设置:

I have the following setup in my MVC 6 ASP.NET 5 project:

Startup.cs在配置方法"中:

Startup.cs in the Configure Method:

app.UseCookieAuthentication(options =>
{
    options.AuthenticationScheme = "Cookie";
    options.LoginPath = new PathString("/<TENANT>/account/signin/");
    options.AccessDeniedPath = new PathString("/<TENANT>/account/unauthorised/");
    options.AutomaticAuthenticate = true;
    options.AutomaticChallenge = true;
    options.Events = new CookieAuthenticationEvents
    {
        OnRedirectToReturnUrl = MyClass.RedirectToReturnUrlAsync
    };
});

事件类:

public static class MyClass
{
    public static async Task RedirectToReturnUrlAsync(CookieRedirectContext context)
    {
        context.Options.LoginPath = new PathString("/<HERE I PLAN TO PUT LOGIC TO FIGURE OUT TENANT FROM CONTEXT>/account/signin");
    }

}

假设用户转到以下网址:

Lets say a user goes to the following url:

http://localhost/mycompany/securecontroller/secureaction

我希望Cookie中间件将用户重定向到:

I want the Cookie middleware to redirect the user to:

http://localhost/mycompany/account/signin

问题是当发生重定向到返回URL"时,代码"MyClass.RedirectToReturnUrlAsync"永远不会被击中,因此我找不到在运行时修改LoginPath的机会.

The problem is the code "MyClass.RedirectToReturnUrlAsync" never gets hit when a Redirect to Return Url happens, so I cannot find the opportunity to modify the LoginPath at runtime.

我怀疑我的设置有问题.有人遇到过这个问题吗?

I suspect I have something wrong in my setup. Has anybody ever encountered this problem?

Hooroo

推荐答案

好的,我想我已经知道了.我从错误的角度看了这个问题(并且睡了一觉!)

Ok, I think I figured it out. I was looking at the problem from the wrong angle (and after a getting some sleep!)

app.UseCookieAuthentication(options =>
{
    options.AuthenticationScheme = "Cookie";
    options.LoginPath = new PathString("/<TENANT>/account/signin/");
    options.AccessDeniedPath = new PathString("/<TENANT>/account/unauthorised/");
    options.AutomaticAuthenticate = true;
    options.AutomaticChallenge = true;
    options.Events = new MyCookieAuthenticationEvents();
});

创建自己的自定义Cookie身份验证事件的正确方法是从CookieAuthenticationEvents对象派生并覆盖您要自定义的事件,如下所示:

The proper way to create your own custom Cookie Authentication Events would be to derive from the CookieAuthenticationEvents object and override the events you'd like to custom, something like this:

public class MyCookieAuthenticationEvents : CookieAuthenticationEvents
{
    public override Task RedirectToLogin(CookieRedirectContext context)
    {
        context.RedirectUri = <PUT LOGIC HERE TO REPLACE YOUR REDIRECT URI>
        return base.RedirectToLogin(context);
    }
}

在之前的尝试中,我也定位了错误的事件.就我而言,要覆盖的正确方法是"RedirectToLogin"方法.

I was also targeting the wrong Event in my previous attempt. In my case, the correct method to override was the "RedirectToLogin" method.

Hooroo

这篇关于修改CookieAuthenticationOptions LoginPath OnRedirectToReturnUrl事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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