0

我的 Razor Pages 应用程序配置如下。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.AddDefaultIdentity<IdentityUser>()
        .AddRoles<IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
    
    services.AddAuthorization(options =>
    {
        options.AddPolicy("RequireAdminRole", policy => 
            policy.RequireAuthenticatedUser().RequireRole("Admin"));
    });

    services.AddMvc()
        .AddRazorPagesOptions(options =>
        {
            options.Conventions.AuthorizePage("/About", "RequireAdminRole");
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    app.UseAuthentication();
    app.UseMvc();
}

我有一个具有“管理员”角色的用户。当用户登录并访问“关于”页面时,我得到以下信息:

拒绝访问

您无权访问此资源。

我究竟做错了什么?

更新

如果我在页面方法中删除AuthorizePageand 使用,然后在页面中输出属性,则显示管理员用户。所以,不知道为什么不工作。GetUsersInRoleAsync("Admin")About.cshtml.csOnGetUserNameAbout.cshtmlAuthorizePage

2017 年 5 月 29 日更新

我的源代码在这个Github Resository

4

4 回答 4

5

我设法找到了解决方案:

services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();

我认为它的工作原理如下:

  • AddItentity - 设置身份。
  • AddDefaultUI - 使用新的 Razor 类库 UI。
  • AddDefaultTokenProviders - 需要两因素身份验证。
于 2018-06-10T11:40:15.643 回答
3

你必须.UseAuthentication().UseMvc() app.UseAuthentication(); app.UseMvc(); 我因为这个而失去很多头发之前。

于 2018-05-29T06:29:16.550 回答
1

请更改这些代码行,然后重试。谢谢

        //Old
        /*services
            .AddDefaultIdentity<IdentityUser>()
            .AddRoles<IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
            */

        //New
        services
            .AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
于 2018-06-04T18:46:16.153 回答
1

上述答案对我不起作用,但在Github上阅读此内容后,我更改了使用 Alan T 解决方案的代码。

services.AddIdentity<IdentityUser, IdentityRole>()
 .AddDefaultUI()
 .AddDefaultTokenProviders()
 .AddEntityFrameworkStores<ApplicationDbContext>();

对此

  services.AddIdentity<IdentityUser, IdentityRole>()
             .AddEntityFrameworkStores<AuthenticationContext>()
          .AddDefaultUI();

之后的 .AddEntityFrameworkStores<AuthenticationContext>() 需要services.AddIdentity<IdentityUser, IdentityRole>()

它完美地工作。我没有使用两因素身份验证,所以我不需要.AddDefaultTokenProviders()

希望它会帮助其他与我在角色方面遇到相同问题的人。

于 2018-08-02T14:16:12.400 回答