在 ASP NET Core 中禁用注册模板

Яро*_*ров 11 c# asp.net-core

如何在 ASP NET Core 2.2.0+ 中禁用注册表单?

只是为了获取和删除适当的模型,我不能,因为它不在项目中,根据文档,我知道这与“ConfigureApplicationPartManager”之类的东西有关

链接在这里

但我找不到合适的例子来禁用它

目标是禁用新用户的注册,只留下登录\密码表单

services.AddMvc()
            .ConfigureApplicationPartManager(x =>
            {


                var thisAssembly = typeof(IdentityBuilderUIExtensions).Assembly;
                var relatedAssemblies = RelatedAssemblyAttribute.GetRelatedAssemblies(thisAssembly, throwOnError: true);
                var relatedParts = relatedAssemblies.ToDictionary(
                    ra => ra,
                    CompiledRazorAssemblyApplicationPartFactory.GetDefaultApplicationParts);
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
Run Code Online (Sandbox Code Playgroud)

Mar*_*dok 20

另一种选择是删除注册链接并在 Startup 类中从注册重定向到登录:

    app.UseEndpoints(endpoints =>
        {
            endpoints.MapGet("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Login", true, true)));
            endpoints.MapPost("/Identity/Account/Register", context => Task.Factory.StartNew(() => context.Response.Redirect("/Identity/Account/Login", true, true)));
        });
Run Code Online (Sandbox Code Playgroud)


Nie*_* R. 12

您可以指定要搭建脚手架的部件。以下是 ASP.NET Core 文档的摘录。链接到下面的来源。

要禁用用户注册:

  • 脚手架标识。包括 Account.Register、Account.Login 和 Account.RegisterConfirmation。例如:
dotnet aspnet-codegenerator identity -dc RPauth.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.RegisterConfirmation"
Run Code Online (Sandbox Code Playgroud)
  • 更新Areas/Identity/Pages/Account/Register.cshtml.cs以便用户无法从该端点注册:
public class RegisterModel : PageModel
{
    public IActionResult OnGet()
    {
        return RedirectToPage("Login");
    }

    public IActionResult OnPost()
    {
        return RedirectToPage("Login");
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 更新Areas/Identity/Pages/Account/Register.cshtml以与前面的更改保持一致:
@page
@model RegisterModel
@{
    ViewData["Title"] = "Go to Login";
}

<h1>@ViewData["Title"]</h1>

<li class="nav-item">
    <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
</li>
Run Code Online (Sandbox Code Playgroud)
  • Areas/Identity/Pages/Account/Login.cshtml注释或删除注册链接
@*
<p>
    <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>
*@
Run Code Online (Sandbox Code Playgroud)
  • 更新区域/身份/页面/帐户/注册确认页面。
    • 从 cshtml 文件中删除代码和链接。
    • 从 中删除确认码PageModel
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
    public IActionResult OnGet()
    {  
        return Page();
    }
}
Run Code Online (Sandbox Code Playgroud)

来源:https : //docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=visual-studio#disable-register-page

更多信息dotnet aspnet-codegeneratorhttps : //docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator