ASP.NET 核心 2.1:Razor 页面 - 基于角色的授权不起作用
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();
}
我有一个具有“管理员”角色的用户。当用户登录并访问“关于”页面时,我得到以下信息:
Access denied
You do not have access to this resource.
我做错了什么?
更新
如果我在About.cshtml.cs
页面OnGet
方法中去掉AuthorizePage
并使用GetUsersInRoleAsync("Admin")
,那么在UserName
属性中输出About.cshtml
页面,显示admin用户。所以,不确定为什么 AuthorizePage
不起作用。
2017 年 5 月 29 日更新
我的源码在这个Github Resository
你必须在 .UseMvc()
之前加上 .UseAuthentication()
app.UseAuthentication();
app.UseMvc();
我为此掉了很多头发。
请更改这些代码行,然后重试。
谢谢
//Old
/*services
.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
*/
//New
services
.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
我找到了解决方案:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
我认为它的工作原理如下:
- AddItentity - 设置身份。
- AddDefaultUI - 使用新的 Razor Class 库 UI。
- AddDefaultTokenProviders - 双重身份验证需要。
以上答案对我不起作用,但在 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()
希望它能帮助那些和我有相同角色问题的人。
我的 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();
}
我有一个具有“管理员”角色的用户。当用户登录并访问“关于”页面时,我得到以下信息:
Access denied
You do not have access to this resource.
我做错了什么?
更新
如果我在About.cshtml.cs
页面OnGet
方法中去掉AuthorizePage
并使用GetUsersInRoleAsync("Admin")
,那么在UserName
属性中输出About.cshtml
页面,显示admin用户。所以,不确定为什么 AuthorizePage
不起作用。
2017 年 5 月 29 日更新
我的源码在这个Github Resository
你必须在 .UseMvc()
之前加上 .UseAuthentication()
app.UseAuthentication();
app.UseMvc();
我为此掉了很多头发。
请更改这些代码行,然后重试。 谢谢
//Old
/*services
.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
*/
//New
services
.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
我找到了解决方案:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
我认为它的工作原理如下:
- AddItentity - 设置身份。
- AddDefaultUI - 使用新的 Razor Class 库 UI。
- AddDefaultTokenProviders - 双重身份验证需要。
以上答案对我不起作用,但在 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()
希望它能帮助那些和我有相同角色问题的人。