C# 在ASP NET核心中禁用注册模板
如何在ASP NET Core 2.2.0+中禁用注册表 仅为了获取并删除适当的模型,我不能,因为它不在项目中,根据文档,我知道这与“ConfigureApplicationPartManager”之类的东西有关 链接 但我找不到合适的例子来禁用它 目标是禁用新用户的注册,只留下登录\密码表单C# 在ASP NET核心中禁用注册模板,c#,asp.net-core,C#,Asp.net Core,如何在ASP NET Core 2.2.0+中禁用注册表 仅为了获取并删除适当的模型,我不能,因为它不在项目中,根据文档,我知道这与“ConfigureApplicationPartManager”之类的东西有关 链接 但我找不到合适的例子来禁用它 目标是禁用新用户的注册,只留下登录\密码表单 services.AddMvc() .ConfigureApplicationPartManager(x => {
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);
可以指定要搭建的零件。以下是ASP.NET核心文档的摘录。链接到下面的来源 禁用用户注册:
- 脚手架标识。包括Account.Register、Account.Login和Account.RegisterConfirmation。例如:
- 更新Areas/Identity/Pages/Account/Register.cshtml.cs,以便用户无法从此端点注册:
- 更新Areas/Identity/Pages/Account/Register.cshtml以与前面的更改保持一致:
有关
dotnet aspnet codegenerator
:的更多信息,另一个选项是删除注册链接并在启动类中从注册重定向到登录:
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)));
});
另一种解决方法是使用中间件,并使用特定的页面模型阻塞所有路由
公共类启动{
// ...
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
// ...
app.UseRouting();
var blockedPages=new[]{
类型(RegisterModel),
类型(注册确认模型),
};
应用程序使用(异步(上下文,下一步)=>
{
var pageActionDescriptor=context.GetEndpoint()?.Metadata.GetMetadata();
var page=(pageActionDescriptor作为CompiledPageActionDescriptor)?.ModelTypeInfo.BaseType;
if(blockedPages.Contains(第页))
{
context.Response.Redirect(“/”);
返回;
}
等待next.Invoke();
});
}
}
看到这个:回答得好!如果出现错误您打算执行.NET程序,但dotnet aspnet codegenerator不存在。
则需要首先通过dotnet工具安装-g dotnet aspnet codegenerator安装它。
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>
*@
[AllowAnonymous]
public class RegisterConfirmationModel : PageModel
{
public IActionResult OnGet()
{
return Page();
}
}
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)));
});