如何在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);
答案 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文档的摘录。链接到下面的源。
要禁用用户注册:
dotnet aspnet-codegenerator identity -dc RPauth.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.RegisterConfirmation"
public class RegisterModel : PageModel
{
public IActionResult OnGet()
{
return RedirectToPage("Login");
}
public IActionResult OnPost()
{
return RedirectToPage("Login");
}
}
@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>
@*
<p>
<a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>
*@
PageModel
中删除确认代码:[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
public IActionResult OnGet()
{
return 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();
});
}
}