基于角色的授权在 Asp.Net Core 2.1 中不起作用
Role-based authorization doesn't work in Asp.Net Core 2.1
根据 Microsoft 文档
,我正在尝试在 Asp.Net Core 2.1 中实施基于角色的授权
这是我的 Startup.cs 的样子:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
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.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 0;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
这里是控制器代码:
[Authorize(Roles = "Admin")]
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
但是,当我尝试通过分配了管理员角色的测试帐户访问此页面时 - 我收到“访问被拒绝”错误。
我已经检查过,在数据库方面,一切都已正确配置 - AspNetRoles table 中有管理员角色,我的用户在 AspNetUserRoles table 中有此角色。我之前分别通过 RoleManager 和 UserManager 向用户添加了角色和连接。
[Authorize] 标签本身也可以正常工作(不允许未授权用户查看页面)。
我的问题是 - 为什么基于角色的授权不起作用?
问题已通过升级到 Asp.Net Core 3.1
得到解决
根据 Microsoft 文档
,我正在尝试在 Asp.Net Core 2.1 中实施基于角色的授权这是我的 Startup.cs 的样子:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
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.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 0;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
这里是控制器代码:
[Authorize(Roles = "Admin")]
public IActionResult About()
{
ViewData["Message"] = "Your application description page.";
return View();
}
但是,当我尝试通过分配了管理员角色的测试帐户访问此页面时 - 我收到“访问被拒绝”错误。 我已经检查过,在数据库方面,一切都已正确配置 - AspNetRoles table 中有管理员角色,我的用户在 AspNetUserRoles table 中有此角色。我之前分别通过 RoleManager 和 UserManager 向用户添加了角色和连接。 [Authorize] 标签本身也可以正常工作(不允许未授权用户查看页面)。 我的问题是 - 为什么基于角色的授权不起作用?
问题已通过升级到 Asp.Net Core 3.1
得到解决