不同域上的ASP.NET Core的CookieAuthenticationAuthenticationOptions.LoginPath [英] ASP.NET Core CookieAuthenticationOptions.LoginPath on different domain
本文介绍了不同域上的ASP.NET Core的CookieAuthenticationAuthenticationOptions.LoginPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用CookieAuthenticationOptions
在.NET Core应用程序中配置身份验证,但是我的登录页面位于其他域上.但是,LoginPath
属性仅允许内部路径,而不允许完整的URI.所以下面的代码:
I'm using CookieAuthenticationOptions
to configure authentication in my .NET Core application, but my login page is on a different domain. However, the LoginPath
property only allows an internal path, not a full URI. So the following code:
var cookieOptions = new CookieAuthenticationOptions()
{
AuthenticationScheme = "Cookies",
LoginPath = new PathString("https://externaldomain.com/login"),
CookieName = string.Format("myCookie"),
};
app.UseCookieAuthentication(cookieOptions);
...无效.这应该相当简单,还是我在这里遗漏了一些东西?我不希望在我的应用程序内部处理此问题,而自己进行实际的重定向.那真是个la脚.
... is invalid. This should be reasonably simple, or am I missing something here? I'd hate to handle this internally in my application and do the actual redirection myself. That would be kinda lame.
推荐答案
使用OnRedirectToLogin
:
var cookieOptions = new CookieAuthenticationOptions()
{
AuthenticationScheme = "Cookies",
CookieName = string.Format("myCookie"),
Events = new CookieAuthenticationEvents()
{
OnRedirectToLogin = async (context) =>
{
context.HttpContext.Response.Redirect("https://externaldomain.com/login");
};
}
}
这篇关于不同域上的ASP.NET Core的CookieAuthenticationAuthenticationOptions.LoginPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文