ASP.NET Core 2.1 标识 : How to remove the Default UI razor pages?

标签 asp.net asp.net-identity razor-pages asp.net-core-2.1

扩展这个问题的答案:
Change routing in ASP.NET Core Identity UI?

Javier recommends one of the following options when wanting to customise the URLs:

  • Use the scaffolding element of the Default UI and make all necessary customisations yourself.
  • Use a redirection rule that points the old routes to the new routes.
  • Don't use the Default UI at all.


在新的 ASP.NET Core 2.1 MVC 项目中,设置了身份验证:个人用户帐户,如何不使用默认 UI?它似乎默认与 Identity Core 一起安装。

enter image description here

项目创建完成后,有什么方法可以去掉默认UI Razor 页面,仍然使用Identity Core?

我可以删除/Identity/吗?区域,并创建我自己的 AccountController反而?

最佳答案

使用 the article linked by Panagiotis Kanavos ,我能够找到解决方案。

从 ASP.NET Core 2.1.0-preview1 开始,有一行 .AddDefaultUI() ,您不必将其包含在 Startup.cs 中.

services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultUI()
    .AddDefaultTokenProviders();

然而,在 Core 2.1 的最终发布版本中,同一部分被简化为:
services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();

解决方案 ,如果你换了AddDefaultIdentity返回 AddIdentity ,您可以覆盖默认值。 IE。不包括 .AddDefaultUI() (也不要搭建 UI 的脚手架),您可以自己编写。
services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
    .AddEntityFrameworkStores<ApplicationDbContext>()
    // .AddDefaultUI()
    .AddDefaultTokenProviders();

然后,我认为删除 /Areas/Identity/ 是安全的文件夹,但我不是 100%

更新:

我清理了我的答案以详细说明我最终采用的最终解决方案,删除 ASP.NET Core 2.1 附带的默认标识 UI Razor 页面,并改用 MVC。

1) 在 Startup.cs ,
    public void ConfigureServices(IServiceCollection services)
    {
        // Unrelated stuff commented out...

        // BEGIN: Identity Setup (Overrides default identity)
        services.AddIdentity<ApplicationUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
        // END: Identity Setup

        services.Configure<IdentityOptions>(options =>
        {
            // Set your identity Settings here (password length, etc.)
        });

        // More unrelated stuff commented out...

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // Added after AddMvc()
        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = $"/account/login";
            options.LogoutPath = $"/account/logout";
            options.AccessDeniedPath = $"/account/access-denied";
        });

        // More unrelated stuff commented out...
    }

显然,同时替换 ApplicationUser , 和 IdentityRole如果需要,可以使用您自己的类(class)。

2) 删除 ASP.NET Core 2.1 项目默认的 Identity 文件夹。

3) 创建一个新的单独的 ASP.NET Core 2.0 项目(不是“2.1”),使用 Individual User Account在项目创建窗口中选择的身份验证。

4) 复制 AccountControllerManageController , 对应 ViewModelsViews ,从 2.0 项目到您的 ASP.NET Core 2.1 项目。

执行上述操作,到目前为止我还没有遇到任何问题。

关于ASP.NET Core 2.1 标识 : How to remove the Default UI razor pages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288801/

相关文章:

Javascript\JQuery 函数在没有警报的情况下无法工作

c# - 用于多个字段的 ASP.NET 验证器

asp.net-mvc - 迁移 asp.net core Identity 以将数据保护与 SQL DB 结合使用

c# - User.Identity.IsAuthenticated 始终返回 false

asp.net - 如何在 ASP.NET CORE Razor Pages 中创建共享表单?

javascript - 使用 jQuery 事件

mysql - 当我尝试使用 MySQL 中的身份存储注册用户时出错

c# - 使用 Razor Pages 在 ASP.NET Core MVC 中使用 Handler 设置 Kendo UI Grid DataSource Read 属性

asp.net-core - ASP.NET Core Razor 页面中的排序和过滤

c# - 我什么时候应该使用 Localize 控件而不是 Literal?