Disable registration template in ASP NET core

Another way to solve it is using Middleware, and blocking all routes with specific PageModels.

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


You can specify which parts to scaffold. The following is an excerpt from the ASP.NET Core documentation. Link to the source below.

To disable user registration:

  • Scaffold Identity. Include Account.Register, Account.Login, and Account.RegisterConfirmation. For example:
dotnet aspnet-codegenerator identity -dc RPauth.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.RegisterConfirmation"
  • Update Areas/Identity/Pages/Account/Register.cshtml.cs so users can't register from this endpoint:
public class RegisterModel : PageModel
{
    public IActionResult OnGet()
    {
        return RedirectToPage("Login");
    }

    public IActionResult OnPost()
    {
        return RedirectToPage("Login");
    }
}
  • Update Areas/Identity/Pages/Account/Register.cshtml to be consistent with the preceding changes:
@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>
  • Comment out or remove the registration link from Areas/Identity/Pages/Account/Login.cshtml
@*
<p>
    <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>
*@
  • Update the Areas/Identity/Pages/Account/RegisterConfirmation page.
    • Remove the code and links from the cshtml file.
    • Remove the confirmation code from the PageModel:
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
    public IActionResult OnGet()
    {  
        return Page();
    }
}

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

More information about dotnet aspnet-codegenerator: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/tools/dotnet-aspnet-codegenerator


Another option is just to remove Register link and redirect from register to login in your Startup class:

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