授权在 ASP.NET Core 2.1 中使用标识 UI 作为库的注册页面



我正在使用 ASP.NET 核心身份在.NET Core 2.1.3中处理Razor Pages项目。

我想知道,是否可以授权"注册"页面,将身份作为预构建的 UI,而无需将所有与身份相关的代码脚手架放回去。只有注册用户(最佳基于角色(才能注册新用户。

我尝试在Startup.cs-file 中添加以下内容,但两者都没有成功:

services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizeFolder("/Admin");
// Trying to authorize the Register-page
options.Conventions.AuthorizeAreaPage("Identity", "/Account/Register");
options.Conventions.AuthorizeAreaPage("Identity", "/Pages/Account/Register");
options.Conventions.AuthorizePage("/Identity/Account/Register");
options.Conventions.AuthorizePage("/Account/Register");
options.Conventions.AuthorizePage("/Identity/Pages/Account/Register");
options.Conventions.AuthorizeFolder("/Identity/Pages/Account");
options.Conventions.AuthorizeFolder("/Identity/Account");
options.Conventions.AuthorizeFolder("/Account");
options.Conventions.AuthorizeFolder("/Areas/Identity/Pages/Account");
options.Conventions.AuthorizeFolder("/Areas/Identity/Pages");
options.Conventions.AuthorizeFolder("/Areas/Identity");
options.Conventions.AuthorizeFolder("/Areas");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

或者,是否可以完全禁用注册新用户?

谢谢!

以下内容对我有用,并使用基于角色的授权。我仍然需要搭建一些页面,但只有那些需要更新的页面。

脚手架登录和注册页面

我仅使用以下方法搭建了登录和注册页面的脚手架:

使用授权将标识基架标识放入 Razor 项目中

这也是有帮助的。有一个演示显示上述内容:

ASP.NET 核心2.1:Build 2018的Web开发中的新功能

Login.cshtml - 我注释掉了以下内容。

<p>
<a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
</p>

Register.cshtml.cs - 我删除了 AllowAnonymous 属性

[AllowAnonymous]
public class RegisterModel : PageModel


启动.cs - 配置服务

public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<IdentityUser, IdentityRole>(options =>{
options.SignIn.RequireConfirmedEmail = true;
})
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
// Register no-op EmailSender used by account confirmation and password reset during development
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
services.AddSingleton<Microsoft.AspNetCore.Identity.UI.Services.IEmailSender, EmailSender>();
services.AddAuthorization(options =>
{
options.AddPolicy("RequireAdministratorRole",
policy => policy.RequireRole("Admin"));
options.DefaultPolicy = options.GetPolicy("RequireAdministratorRole");
});
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/About", "RequireAdministratorRole");
options.Conventions.AuthorizeAreaPage("Identity", "/Account/Register");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

相关内容

  • 没有找到相关文章

最新更新