简体   繁体   English

ASP.NET Core Identity更改登录URL

[英]ASP.NET Core Identity change login URL

I'm using ASP.NET Core 2.1 and I used scaffolding to add Identity, which is working OK Except that when I try to go to a page that requires login, it takes me to: /Identity/Account/Login?ReturnUrl 我正在使用ASP.NET Core 2.1并且我使用脚手架添加身份,这是正常工作除了当我尝试转到需要登录的页面时,它需要我: /Identity/Account/Login?ReturnUrl

How do I change it to go to just /Account/Login which is my own login page i created. 如何更改它以转到/我的帐户/登录,这是我自己创建的登录页面。

I tried this: 我试过这个:

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;
                });

but it still goes to /Identity/ 但它仍然是/ Identity /

Check how you register Identity service if you have: 如果您有以下内容,请检查如何注册身份服务

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

replace it with 用它替换它

services.AddIdentity<IdentityUser, IdentityRole>(options => { }) .AddEntityFrameworkStores<ApplicationDbContext>(); and keep your code in the ConfigureApplicationCookie that worked in my case. 并将您的代码保存在我的案例中的ConfigureApplicationCookie中。

I just ran into this same issue. 我刚遇到同样的问题。 I resolved it by moving my 我通过移动我解决了它

services.ConfigureApplicationCookie call to after my services.AddIdentity call in ConfigureServices services.ConfigureApplicationCookie在我的services.AddIdentity调用ConfigureServices之后调用

Try adding new PathString("...") and setting the route in the controller. 尝试添加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();
    }
}

Move services.ConfigureApplicationCookie after services.AddIdentity and most important remove AddDefaultUI in services. 在services.AddIdentity之后移动services.ConfigureApplicationCookie,最重要的是删除服务中的AddDefaultUI。 Reference here 参考这里

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

            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM