簡體   English   中英

ASP.NET Core 2.1:Razor Pages - 基於角色的授權不起作用

[英]ASP.NET Core 2.1: Razor Pages - role based authorisation not working

我的 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();
}

我有一個具有“管理員”角色的用戶。 當用戶登錄並訪問“關於”頁面時,我得到以下信息:

拒絕訪問

您無權訪問此資源。

我究竟做錯了什么?

更新

如果我刪除AuthorizePage和使用GetUsersInRoleAsync("Admin")About.cshtml.cs頁面OnGet方法,然后輸出UserName在屬性About.cshtml頁,顯示管理員用戶。 所以,不確定為什么AuthorizePage不起作用。

2017 年 5 月 29 日更新

我的源代碼在這個Github 資源庫中

我設法找到了解決方案:

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

我認為它的工作原理如下:

  • AddItentity - 設置身份。
  • AddDefaultUI - 使用新的 Razor 類庫 UI。
  • AddDefaultTokenProviders - 需要兩因素身份驗證。

你必須把.UseAuthentication()之前.UseMvc() app.UseAuthentication(); app.UseMvc(); app.UseAuthentication(); app.UseMvc(); 因為這個,我掉了很多頭發。

請更改這些代碼行,然后重試。 謝謝

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

        //New
        services
            .AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

上述答案對我不起作用,但在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()

希望它會幫助其他與我在角色上遇到相同問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM