首页 > 解决方案 > ASP.NET Core 2.1 标识:如何删除默认 UI 剃须刀页面?

问题描述

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

当想要自定义 URL 时,Javier 建议使用以下选项之一:

  • 使用默认 UI 的脚手架元素并自己进行所有必要的自定义。
  • 使用将旧路由指向新路由的重定向规则。
  • 根本不要使用默认 UI。

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

在此处输入图像描述

项目创建后,去掉 Default UI razor pages 的方法是什么,仍然使用 Identity Core?

我可以删除该/Identity/区域,然后创建自己的区域AccountController吗?

标签: asp.netasp.net-identityrazor-pagesasp.net-core-2.1

解决方案


使用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 剃须刀页面,并改用 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...
    }

显然,如果需要,请将两者替换ApplicationUserIdentityRole您自己的类。

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

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

4) 将 2.0 项目中的AccountControllerand 和ManageController对应的ViewModelsand复制Views到您的 ASP.NET Core 2.1 项目中。

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


推荐阅读