簡體   English   中英

ASP.NET Core Identity更改登錄URL

[英]ASP.NET Core Identity change login URL

我正在使用ASP.NET Core 2.1並且我使用腳手架添加身份,這是正常工作除了當我嘗試轉到需要登錄的頁面時,它需要我: /Identity/Account/Login?ReturnUrl

如何更改它以轉到/我的帳戶/登錄,這是我自己創建的登錄頁面。

我試過這個:

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 /

如果您有以下內容,請檢查如何注冊身份服務

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

用它替換它

services.AddIdentity<IdentityUser, IdentityRole>(options => { }) .AddEntityFrameworkStores<ApplicationDbContext>(); 並將您的代碼保存在我的案例中的ConfigureApplicationCookie中。

我剛遇到同樣的問題。 我通過移動我解決了它

services.ConfigureApplicationCookie在我的services.AddIdentity調用ConfigureServices之后調用

嘗試添加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();
    }
}

在services.AddIdentity之后移動services.ConfigureApplicationCookie,最重要的是刪除服務中的AddDefaultUI。 參考這里

 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