ASP.NET Core 2.1身份:如何删除默认UI剃刀页面?

Wel*_*ing 19 asp.net asp.net-identity razor-pages asp.net-core-2.1

扩展此问题的答案: 在ASP.NET Core Identity UI中更改路由?

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

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

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

在此输入图像描述

创建项目后,删除默认UI剃刀页面的方法是什么,仍然使用Identity Core?

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

Wel*_*ing 21

使用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();
Run Code Online (Sandbox Code Playgroud)

然而,在Core 2.1的最终版本中,相同的部分被简化为:

services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<ApplicationDbContext>();
Run Code Online (Sandbox Code Playgroud)

解决方案,如果更改AddDefaultIdentityAddIdentity,您可以覆盖默认值.IE不包括.AddDefaultUI()(也不支持UI),你可以编写自己的.

services.AddIdentity<IdentityUser, IdentityRole>(options => options.Stores.MaxLengthForKeys = 128)
    .AddEntityFrameworkStores<ApplicationDbContext>()
    // .AddDefaultUI()
    .AddDefaultTokenProviders();
Run Code Online (Sandbox Code Playgroud)

然后,我认为删除/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...
    }
Run Code Online (Sandbox Code Playgroud)

显然,如果需要,可以替换它们ApplicationUser,并IdentityRole使用您自己的类.

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项目.

做到这一点,我到目前为止还没有遇到任何问题.

  • 我觉得奇怪的是,当您创建 Angular 应用程序或任何基于 API 的应用程序时,项目模板没有为您提供选择旧控制器样式的选项。不管怎样,客户端都会发布数据,所以几乎没有什么好处(我可以看到)迫使开发人员要么使用 RazorPages 并进行不必要的覆盖,要么花时间删除新的并重新安装旧的。项目模板不是应该帮助我们吗? (3认同)
  • 您可能还需要配置默认的`EmailService`。`services.AddSingleton &lt;IEmailService,EmailService&gt;()` (2认同)

hiv*_*510 5

有点晚了,但有一个更简单的方法来做到这一点。您可以添加新的脚手架来覆盖所有内容。看看这篇文章