- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的 MVC 6 ASP.NET 5 项目中有以下设置:
配置方法中的Startup.cs:
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");
}
}
假设用户转到以下 url:
http://localhost/mycompany/securecontroller/secureaction
我希望 Cookie 中间件将用户重定向到:
http://localhost/mycompany/account/signin
问题是代码“MyClass.RedirectToReturnUrlAsync”在重定向到返回 Url 时永远不会被命中,所以我找不到在运行时修改 LoginPath 的机会。
我怀疑我的设置有问题。有人遇到过这个问题吗?
呼呼
最佳答案
好的,我想我明白了。我从错误的角度看问题(在睡了一觉之后!)
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 对象并覆盖您想要自定义的事件,如下所示:
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”方法。
呼呼
关于c# - 修改 CookieAuthenticationOptions LoginPath OnRedirectToReturnUrl 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36663763/
我有以下代码: public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new
在我的应用程序的 Asp.Net Identity Auth 中间件设置中,我有 app.UseCookieAuthentication(new CookieAuthenticationOptions
我的 MVC 6 ASP.NET 5 项目中有以下设置: 配置方法中的Startup.cs: app.UseCookieAuthentication(options => { options.
我正在使用 1.0.1 版本的 asp.net core 并且在我的表单中使用身份验证。 我使用 UseCookieAuthentication 并给出错误 Cannot convert lambda
在 HttpContext.SignInAsync 和 Microsoft.AspNetCore.Authentication.Cookies 中传递给 Microsoft.AspNetCore.Au
我正在使用 OWIN 中间件进行 cookie 身份验证和 openIdConnect。在我将 openIdConnect 身份验证添加到我的启动身份验证代码和 cookie 身份验证选项之前,Log
在 ASP.NET Core 2.1 中,谁能解释一下 CookieAuthenticationOptions.LogoutPath 的作用?每the documentation它说: If the
我在 .NET MVC 4.5 设置中使用 OWIN 的 cookie 身份验证。我在 Startup.Auth.cs (下面的代码)中设置了 cookie 身份验证配置,并且我想访问我在 Contr
我是一名优秀的程序员,十分优秀!