Asp.Net MVC vNext身份登录路由更改无法正常工作

时间:2015-11-29 20:36:37

标签: asp.net-core-mvc

我试图为登录,退出等设置新路线

现在,当我使用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")
});
除了路由到我的主页并显示空白页面之外它没有做任何事情(我假设它没有被授权)。

这样做的正确方法是什么?

1 个答案:

答案 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()