我花了很多时间在这件事上,看起来很简单,但找不到解决方案。创建一个项目并运行良好,登录,注册等。但授权不适用于角色。创建并设置角色:但尝试访问时总是返回 Access Denied:public class _ConfigurationsController : Controller{ [Authorize(Roles = "AdminApp")] public IActionResult Index() { return View(); }}启动.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.UseSqlServer(Configuration.GetConnectionString("MyConnection"))); services.AddDbContext<Scaffolding_AutoGer_Context>(options => options.UseSqlServer(Configuration.GetConnectionString("MyConnection"))); services.AddDefaultIdentity<IdentityUser>() .AddRoles<IdentityRole>() .AddRoleManager<RoleManager<IdentityRole>>() .AddDefaultTokenProviders() .AddEntityFrameworkStores<ApplicationDbContext>(); services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);}public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider){ 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?}"); });}AspNet用户表AspNetUserRoles 表MVC - 脚手架项目个人账户登录.NET Core 2.1 VS 2017
1 回答
叮当猫咪
TA贡献1776条经验 获得超12个赞
我认为您的问题与没有配置策略有关。在public void ConfigureServices(IServiceCollection services)
指定这些。
services.AddAuthorization(options => options.AddPolicy("AdminApp", policy => policy.RequireClaim("Manager")));
- 1 回答
- 0 关注
- 241 浏览
添加回答
举报
0/150
提交
取消