首页 > 解决方案 > 使用身份更改 .NET Core 2.2 中的登录/注销页面

问题描述

我在 .net 核心的每个版本中都以不同的方式执行此操作,但是现在在 2.2 中,我无法让我的代码正确重定向。

根据文档,这是我在startup.cs中使用的内容:

(这是一个默认的新建和干净的项目)

services.AddMvc()
    .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
    .AddRazorPagesOptions(options =>
    {
      options.AllowAreas = true;
      options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
      options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
    });

services.ConfigureApplicationCookie(options =>
  {
    options.LoginPath = $"/Identity/Account/Login";
    options.LogoutPath = $"/Identity/Account/Logout";
    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
  });

更改登录或注销路径目前没有任何作用。这里有什么关于我可能遗漏的建议吗?

标签: asp.net-core.net-coreasp.net-identityasp.net-core-2.2

解决方案


好的!问题已确定。如果您使用.AddDefaultUI()身份服务注册,则覆盖默认值options.LoginPath将不起作用。因此,要将您的自定义登录路径用于未经授权的用户重定向,请注释掉.AddDefaultUI()以下内容:

services.AddIdentity<ApplicationUser, IdentityRole>()
                //.AddDefaultUI(UIFramework.Bootstrap4) <-- you have to comment out this
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

现在它应该可以工作了。这是Github 问题


推荐阅读