razor - 使用 Identity UI 作为库在 ASP.NET Core 2.1 中授权注册页面

标签 razor asp.net-core asp.net-identity

我正在使用 ASP.NET Core Identity 在 .NET Core 2.1.3 中开发 Razor Pages 项目。

我想知道是否可以使用身份 as a prebuilt UI 授权“注册”页面,无需将所有身份相关代码搭建起来。只有注册用户(最好基于角色)才能够注册新用户。

我尝试在 Startup.cs 文件中添加以下内容,但都没有成功:

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

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

谢谢!

最佳答案

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

脚手架登录和注册页面

我仅使用以下内容搭建登录和注册页面:

Scaffold identity into a Razor project with authorization

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

What's new in Web Development with ASP.NET Core 2.1 : Build 2018

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

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

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

<s>[AllowAnonymous]</s>
public class RegisterModel : PageModel


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

关于razor - 使用 Identity UI 作为库在 ASP.NET Core 2.1 中授权注册页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50743667/

相关文章:

c# - ASP.Net Identity 与 WebForms 到现有 SQL 数据库

asp.net-mvc - ASP.NET MVC 禁用 Razor 语法的单选按钮

c# - 与 Nancy 一起使用 Razor 进行智能感知

html - 使用 foreach 语句可能会弄乱我的 CSS

c# - 使用 Entity Framework 在 ASP.NET Core 应用程序中对远程用户进行 Windows 身份验证

azure - 从 VS2015 开始将 WebApp 部署到 Azure,实现零停机

c# - DropDownListFor 回调或 if 语句

c# - 为什么在增加用户时使用 IHttpClientFactory 会出现 .NET Core API 性能问题?

c# - 我可以在 ASP.NET Core 中使用 Content Negotiation 向浏览器返回 View 并向 API 调用返回 JSON 吗?

c# - ASP.NET 身份并发登录