ASP.NET Core Identity更改登录URL

时间:2018-05-31 22:55:48

标签: .net asp.net-core .net-core asp.net-identity

我使用ASP.NET Core 2.1并使用脚手架添加身份,这是正常工作除了当我尝试转到需要登录的页面时,它需要我: /Identity/Account/Login?ReturnUrl

如何将其更改为/ account / Login,这是我自己创建的登录页面。

我试过了:

services.ConfigureApplicationCookie(options =>
                {
                    options.AccessDeniedPath = "/Account/AccessDenied";
                    options.Cookie.Name = "Cookie";
                    options.Cookie.HttpOnly = true;
                    options.ExpireTimeSpan = TimeSpan.FromMinutes(720);
                    options.LoginPath = "/Account/Login";
                    options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                    options.SlidingExpiration = true;
                });

但它仍然转到/ Identity /

5 个答案:

答案 0 :(得分:4)

如果有,请检查如何注册身份服务:

services.AddDefaultIdentity<IdentityUser>(options => { }) .AddEntityFrameworkStores<ApplicationDbContext>();

替换

services.AddIdentity<IdentityUser, IdentityRole>(options => { }) .AddEntityFrameworkStores<ApplicationDbContext>(); 并将您的代码保存在适用于我的情况的ConfigureApplicationCookie中。

答案 1 :(得分:4)

我只是遇到了同样的问题。我通过移动

解决了该问题 在我在services.ConfigureApplicationCookie的{​​{1}}通话之后

services.AddIdentity通话

答案 2 :(得分:3)

尝试添加new PathString("...")并在控制器中设置路线。

services.ConfigureApplicationCookie(options =>
{
    options.AccessDeniedPath = new PathString("/Account/AccessDenied");
    options.Cookie.Name = "Cookie";
    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(720);
    options.LoginPath = new PathString("/Account/Login");
    options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
    options.SlidingExpiration = true;
});

[AllowAnonymous]
[Route("Account")]
public class SecurityController : Controller
{
    [Route("Login/{returnUrl?}")]
    public IActionResult Login(string returnUrl = null)
    {
        ViewData["ReturnUrl"] = returnUrl;

        return View();
    }
}

答案 3 :(得分:2)

在services.AddIdentity之后移动services.ConfigureApplicationCookie,最重要的是在服务中删除AddDefaultUI。参考here

答案 4 :(得分:0)

 services.ConfigureApplicationCookie(options =>
            {
                options.Events = new CookieAuthenticationEvents
                {
                    OnRedirectToLogin = x =>
                    {
                        x.Response.Redirect("Account/Login");
                        return Task.CompletedTask;
                    }
                };

            });
相关问题