我试图为登录,退出等设置新路线
现在,当我使用app.UseIdentity()
时,它会自动路由到/Account/Login
。
当我尝试注释掉app.UseIdentity并添加
时MapRoute(
name: "Login",
defaults: new { controller = "Security", action = "Login" },
template: "Login/{returnUrl?}")
和
//app.UseIdentity();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies",
LoginPath = new PathString("/Login"),
LogoutPath = new PathString("/Logout")
});
除了路由到我的主页并显示空白页面之外它没有做任何事情(我假设它没有被授权)。
这样做的正确方法是什么?
答案 0 :(得分:0)
通过浏览Asp.Net来源看出来:
https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNet.Identity/IdentityCookieOptions.cs
public static class MyIdentityExtensions
{
public static IApplicationBuilder UseMyIdentity(this IApplicationBuilder app)
{
if (app == null) {
throw new ArgumentNullException(nameof(app));
}
var options = app.ApplicationServices.GetRequiredService<IOptions<IdentityOptions>>().Value;
app.UseCookieAuthentication(options.Cookies.ExternalCookie);
app.UseCookieAuthentication(options.Cookies.TwoFactorRememberMeCookie);
app.UseCookieAuthentication(options.Cookies.TwoFactorUserIdCookie);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
LoginPath = new PathString("/Login"), // REPLACED THIS
LogoutPath = new PathString("/Logout"), // ADDED THIS
AuthenticationScheme = options.Cookies.ApplicationCookieAuthenticationScheme,
AutomaticAuthenticate = true,
AutomaticChallenge = true,
Events = new CookieAuthenticationEvents
{
OnValidatePrincipal = SecurityStampValidator.ValidatePrincipalAsync
}
});
return app;
}
}
在Startup.cs
app.UseMyIdentity()