[Authorize(Roles = "Admin")]
不适合我。
在startup.cs
( ConfigureServices
) 中,我有:
services.AddDbContextPool<AppDbContext>(
options => options.UseSqlServer(Configuration.GetConnectionString("defaultCon")));
services.AddAuthentication().AddCookie();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<AppDbContext>()
.AddErrorDescriber<CustomIdentityErrorDescriber>()
.AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>();
在Configure
我的方法中:
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Employee}/{action=list}/{id?}")
.RequireAuthorization();
});
我不知道我的错误是什么。