禁用ASP NET Core中的注册模板

时间:2019-03-29 06:43:21

标签: c# asp.net-core

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

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

链接here

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

目标是禁用新用户注册,仅保留“登录名\密码”表单

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);

3 个答案:

答案 0 :(得分:4)

另一种选择是删除注册链接并从注册重定向到您的启动类中的登录:

    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)));
        });

答案 1 :(得分:1)

您可以指定要支撑的零件。以下是ASP.NET Core文档的摘录。链接到下面的源。

要禁用用户注册:

  • 脚手架身份。包括Account.Register,Account.Login和Account.RegisterConfirmation。例如:
dotnet aspnet-codegenerator identity -dc RPauth.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.RegisterConfirmation"
  • 更新区域/身份/页面/帐户/Register.cshtml.cs ,以便用户无法从该端点进行注册:
public class RegisterModel : PageModel
{
    public IActionResult OnGet()
    {
        return RedirectToPage("Login");
    }

    public IActionResult OnPost()
    {
        return RedirectToPage("Login");
    }
}
  • 更新区域/身份/页面/帐户/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>
  • 注释或删除 Areas / Identity / Pages / Account / Login.cshtml
  • 中的注册链接
@*
<p>
    <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>
*@
  • 更新区域/身份/页面/帐户/注册确认页面。
    • 从cshtml文件中删除代码和链接。
    • PageModel中删除确认代码:
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
    public IActionResult OnGet()
    {  
        return Page();
    }
}

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

有关dotnet aspnet-codegenerator的更多信息:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator

答案 2 :(得分:1)

另一种解决方法是使用中间件,并使用特定的 PageModel 阻止所有路由。

public class Startup {
    // ...
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...
        app.UseRouting();

        var blockedPages = new [] {
            typeof(RegisterModel),
            typeof(RegisterConfirmationModel),
        };
        app.Use(async (context, next) =>
        {
            var pageActionDescriptor = context.GetEndpoint()?.Metadata.GetMetadata<PageActionDescriptor>();
            var page = (pageActionDescriptor as CompiledPageActionDescriptor)?.ModelTypeInfo.BaseType;

            if (blockedPages.Contains(page))
            {
                context.Response.Redirect("/");
                return;
            }
            await next.Invoke();
        });
    }
}