Asp.net

ASP.NET Core 2.1 標識:如何刪除預設 UI 剃須刀頁面?

  • July 11, 2018

擴展這個問題的答案: 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嗎?

使用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您自己的類。

  1. 刪除 ASP.NET Core 2.1 項目預設提供的 Identity 區域文件夾。

  2. 創建一個新的單獨的 ASP.NET Core 2.0 項目(不是“2.1”),Individual User Account在項目創建視窗中選擇身份驗證。

  3. 將 2.0 項目中的AccountControllerand 和ManageController對應的ViewModelsand複製Views到您的 ASP.NET Core 2.1 項目中。

執行上述操作,到目前為止我還沒有遇到任何問題。

引用自:https://stackoverflow.com/questions/51288801