c# - .NET Core Identity as UI 取消注册

标签 c# asp.net-core .net-core asp.net-core-mvc

我想在 .NET Core 2.1 + Identity as UI 应用程序中取消“注册”选项。

我当然可以简单地从页面上删除按钮,问题是 - 这样安全吗?

如果不是,我的其他选择是什么?我应该使用脚手架生成注册代码然后在那里禁用它吗?

(同样适用于 SetPassword 等)

谢谢

编辑:似乎有关此的信息已添加到此处:https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.1&tabs=visual-studio#disable-register-page

最佳答案

不幸的是,其他两个答案是不正确的 - 这个问题实际上是指新的 AddDefaultIdentity() 扩展,它使用 Razor 页面来提供默认 UI。确实解决此问题的答案将不会删除问题中要求的注册功能。

背景

AddDefaultIdentity 的工作方式与 AddIdentity 类似,但还包括对 AddDefaultUI 的调用,这使您的应用可以访问新的 Identity razor View (目前有 28 个),这些 View 位于新的 razor 类库中。请注意,这不是 AddDefaultIdentity 和 AddIdentity 之间的唯一区别(见下文)。

为了更改默认 View ,您需要覆盖(“脚手架”)项目中的 View ,然后您可以修改它们。如果您不覆盖 View ,或者如果您覆盖它们然后删除 cshtml 文件,您将简单地返回到默认 UI 版本!即使您删除了指向例如注册,如果用户猜到了 URL,他们仍然可以导航到默认的注册 View 。

选项 1 - 覆盖 View

如果您想保留一些默认 View 并修改或删除其他 View ,您可以按如下方式覆盖 View (from this doc):

  1. 右键单击您的项目 > 添加 > 新建脚手架项
  2. 在“添加基架”对话框的左侧 Pane 中,选择“标识”>“添加”
  3. 在“添加身份”对话框中,选择所需的选项

您现在可以简单地更改您覆盖的 View 的外观和功能,或者要“删除”它,您可以让它返回 404 或重定向到其他地方。如果您删除这个覆盖的 View ,默认 UI 将会恢复!

如果您想覆盖所有 View ,这种方法很快就会变得困惑。

选项 2 - 不添加默认 UI

另一种选择是返回到不调用 AddDefaultUI 的添加身份的旧方法,缺点是您将需要自己添加所有 View 。您可以按如下方式执行此操作(from this doc - 尽管忽略了关于覆盖所有 View 的第一行,但它适用于上面的选项 1):

//remove this: services.AddDefaultIdentity<IdentityUser>()
//use this instead to get the Identity basics without any default UI:
services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

//this assumes you want to continue using razor views for your identity UI
//it specifies areas can be used with razor pages and then adds an 
//authorize filter with a default policy for the folder /Account/Manage and
//the page /Account/Logout.cshtml (both of which live in Areas/Identity/Pages)
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddRazorPagesOptions(options =>
    {
        options.AllowAreas = true;
        options.Conventions.AuthorizeAreaFolder("Identity", "/Account/Manage");
        options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
    });

//configures the application cookie to redirect on challenge, etc.
services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = $"/Identity/Account/Login";
    options.LogoutPath = $"/Identity/Account/Logout";
    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});

//configures an email sender for e.g. password resets
services.AddSingleton<IEmailSender, EmailSender>();

请注意,我不是 100% 相信第二种方法也没有问题,如上所述,AddDefaultIdentity 和 AddIdentity 之间还有其他差异。例如,后者添加了 RoleManager 服务,而前者则没有。此外,我不清楚这两种方法是否会在未来得到同样的支持和维护。

如果对上述选项的作用有疑问(如果您有几个小时的时间),您可以查看 the source for AddDefaultIdentity (也称为 AddIdentityCookiesAddIdentityCore )与旧的 AddIdentity 相比.

选项 3 - 混合方法

目前最好的选择可能是将前两者结合起来,方式如下:

  1. 设置您的项目以使用默认身份
  2. 仅搭建您想要包含的 View 并相应地对其进行编辑
  3. 切换到旧的 AddIdentity 调用并包含选项 2 中所示的 Razor 选项(根据您包含的 View 进行必要的调整

您现在只有您想要的 View ,它们基于默认实现,这意味着这些 View 的大部分工作都已为您完成。

关于c# - .NET Core Identity as UI 取消注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615597/

相关文章:

asp.net-core - 为什么 .Net Core 有自己的声明类型?

azure - ASP .NET Core 和 Azure 表存储

azure - 为什么 host.json 没有被复制到 F# Azure Functions 项目中的输出?

c# - EmitCalli .net 核心替代方案

javascript - 是否有与 Node.js 一起使用的stream.on ('data' )类似的.NET Core?

c# - 在逐字字符串文字中进行替换?

javascript - 从代码隐藏调用 js 函数(不是 startupScript)

c# - Dapper:单元测试 SQL 查询

c# - 单击完成按钮时调用方法 jquery-steps

c# - Web-Api dotnet core 2.2在centos 7 mysql上运行不工作