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

时间:2018-05-26 14:25:28

标签: c# asp.net-core asp.net-core-identity asp.net-core-2.1

我的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 Resository

4 个答案:

答案 0 :(得分:5)

我设法找到解决方案:

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

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

  • AddItentity - 设置标识。
  • AddDefaultUI - 使用新的Razor类库UI。
  • AddDefaultTokenProviders - 双因素身份验证所需。

答案 1 :(得分:2)

您必须在.UseAuthentication()之前加.UseMvc() app.UseAuthentication(); app.UseMvc(); 因为这个原因,我失去了很多头发。

答案 2 :(得分:1)

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

<div ng-app ng-controller="TestController">
  <select>
    <option ng-repeat="n in myNumberArray" value="{{$index+1}}">{{$index+1}}</option>
  </select>
</div>

对此

function TestController($scope) {
    $scope.myNumberArray = new Array(5);
}

services.AddIdentity<IdentityUser, IdentityRole>() .AddDefaultUI() .AddDefaultTokenProviders() .AddEntityFrameworkStores<ApplicationDbContext>(); 必须紧跟 services.AddIdentity<IdentityUser, IdentityRole>() .AddEntityFrameworkStores<AuthenticationContext>() .AddDefaultUI();

之后

它完美地工作。我没有使用两个因素身份验证,因此不需要.AddEntityFrameworkStores<AuthenticationContext>()

希望它能帮助其他在角色方面遇到同样问题的人。

答案 3 :(得分:0)

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

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

        //New
        services
            .AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
相关问题