我正在尝试为登录、注销等设置一个新的路由。
现在,当我使用app.UseIdentity()
时,它会自动路由到/Account/Login
。
当我尝试评论应用程序时。使用标识并添加
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")
});
它不做任何事情,除了路由到我的主页并显示一个空白页面(我认为是因为它未经授权)。
正确的方法是什么?
通过查看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()