Asp.net core 是否可以在.net core中支持多个登录路径标识?
我正在Asp.net Core 3.0中使用identity。我有两个登录页面,比如Asp.net core 是否可以在.net core中支持多个登录路径标识?,asp.net-core,asp.net-web-api,.net-core,cookies,asp.net-core-3.1,Asp.net Core,Asp.net Web Api,.net Core,Cookies,Asp.net Core 3.1,我正在Asp.net Core 3.0中使用identity。我有两个登录页面,比如(/Account/AdminLogin)和(/Account/UserLogin),还有两个角色Admin和User 我的问题是,当我在控制器中使用[Authorize]标记时,当会话结束时,它会在(/Account/AdminLogin)页面中返回给我,因为它是在启动文件中设置的 对于Admin登录,应该在注销URL(/Account/AdminLogin) 对于用户登录,应在注销URL(/Account/U
(/Account/AdminLogin)
和(/Account/UserLogin)
,还有两个角色Admin
和User
我的问题是,当我在控制器中使用[Authorize]
标记时,当会话结束时,它会在(/Account/AdminLogin)
页面中返回给我,因为它是在启动
文件中设置的
对于Admin
登录,应该在注销URL(/Account/AdminLogin)
对于用户
登录,应在注销URL(/Account/UserLogin)
您可以覆盖选项上的
OnRedirectToLogin
事件。
像这样的
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.Name = "Cookie";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(15);
options.LoginPath = "/Account/UserLogin";
options.AccessDeniedPath = "/Account/AccessDenied";
options.LogoutPath = "/Account/Logout";
options.SlidingExpiration = true;
options.Events.OnRedirectToLogin = context =>
{
if (IsAdminContext(context))
{
var redirectPath = new Uri(context.RedirectUri);
context.Response.Redirect("/Account/AdminLogin" + redirectPath.Query);
}
else
{
context.Response.Redirect(context.RedirectUri);
}
return Task.CompletedTask;
};
});
在IsAdminContext
中,您需要区分对于给定的请求,您希望将用户转发到普通用户登录还是管理员登录。可能的实现可能如下所示。其中,对于所有以/admin开头的URL,您将转发到管理员登录
private static bool IsAdminContext(RedirectContext<CookieAuthenticationOptions> context)
{
return context.Request.Path.StartsWithSegments("/admin");
}
私有静态bool IsAdminContext(重定向上下文)
{
返回context.Request.Path.StartsWithSegments(“/admin”);
}
这不是一个好方法,有时我们没有像/admin这样的URL。在这种情况下,这种方法是失败的。基于什么标准,您可以区分要显示的登录?用户尚未登录,因此需要基于其他内容。可以使用什么?我认为这是一个更好的解决方案是的,如果您想拥有完全独立的模式,这可能会对您更好。在您的示例中,您没有显示是否使用特定策略,以及是否需要使用特定策略。您的答案也不错,但我不想写更多的条件(控制器),比如StartsWithSegments(“/admin”)| | StartsWithSegments(“/account”)。。。。。
private static bool IsAdminContext(RedirectContext<CookieAuthenticationOptions> context)
{
return context.Request.Path.StartsWithSegments("/admin");
}