【发布时间】:2020-08-27 21:05:57
【问题描述】:
我使用 asp.net 核心中的默认反应模板和个人用户帐户创建了一个反应项目。 [Authorize] 属性工作正常,但是当我尝试实现 [Authorize(Roles = "Administrator")] 时,我得到一个 403 状态码。
我已添加 ProfileService 以添加声明。
ProfileService.cs
public class ProfileService : IProfileService
{
protected UserManager<ApplicationUser> mUserManager;
public ProfileService(UserManager<ApplicationUser> userManager)
{
mUserManager = userManager;
}
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
ApplicationUser user = await mUserManager.GetUserAsync(context.Subject);
IList<string> roles = await mUserManager.GetRolesAsync(user);
IList<Claim> roleClaims = new List<Claim>();
foreach (string role in roles)
{
roleClaims.Add(new Claim(JwtClaimTypes.Role, role));
}
context.IssuedClaims.AddRange(roleClaims);
}
public Task IsActiveAsync(IsActiveContext context)
{
return Task.CompletedTask;
}
}
“角色”:“管理员”存在于我的 JWT 令牌中。
我已将 Authorize 属性添加到我的控制器中。
[Authorize(Roles = "Administrator")]
[HttpGet]
public async Task<ActionResult<IEnumerable<Order>>> GetOrders()
{
return await _context.Orders.ToListAsync();
}
我还配置了我的 Startup.cs,如下所示。
Startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
//services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
// .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddRoleManager<RoleManager<IdentityRole>>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, ApplicationDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddAuthorization();
services.Configure<IdentityOptions>(options =>
{
options.ClaimsIdentity.RoleClaimType = JwtClaimTypes.Role;
});
services.AddTransient<IProfileService, ProfileService>();
services.AddControllersWithViews();
services.AddRazorPages();
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
不确定我哪里出了问题。
【问题讨论】:
-
我猜是声明映射起作用了,您将角色添加为 JwtClaimTypes.Role,这是配置文件服务中的“角色”,但 Authorize 属性很可能使用 ClaimTypes.Role,即“ schemas.microsoft.com/ws/2008/06/identity/claims/role" 所以也许您需要查看声明映射?
-
@Brendon Lee 你得到这个工作了吗?我觉得我接近让角色在我的 dotnet5 和 dotnet6 反应(使用身份)项目中工作,但我希望有一个示例项目来查看它的运行情况并找出我哪里出错了。
标签: c# reactjs asp.net-core identityserver4