IdentityServer4 指定角色授权(Authorize(Roles=amp;quot;adminamp;quot;))
1. 業(yè)務(wù)場景
IdentityServer4 授權(quán)配置Client中的AllowedScopes,設(shè)置的是具體的 API 站點名字,也就是使用方設(shè)置的ApiName,示例代碼:
new Client
{
? ?ClientId = "client_id_1",
? ?AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
? ?AllowOfflineAccess = true,
? ?AccessTokenLifetime = 3600 * 6, //6小時
? ?SlidingRefreshTokenLifetime = 1296000, //15天
? ?ClientSecrets =
? ?{
? ? ? ?new Secret("secret".Sha256())
? ?},
? ?AllowedScopes =
? ?{
? ? ? ?"api_name1"
? ?},
}
//API 服務(wù)配置
app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
{
? ?Authority = $"http://localhost:5000",
? ?ApiName = "api_name1",
? ?RequireHttpsMetadata = false
});
上面兩個api_name1配置要一致,問題來了,因為授權(quán)中心的scope配置是整個 API 服務(wù),如果我們存在多個Client配置,比如一個前臺和后臺,然后都需要訪問api_name1,就會出現(xiàn)一些問題。
比如,api_name1服務(wù)中的一個接口服務(wù)配置代碼:
[Authorize()] [Route("api/values")] [HttpGet]public IActionResult Get(){ ??return Ok(); }
Authorize()的配置,說明api/values接口需要授權(quán)后訪問,如果授權(quán)中心配置了兩個Client(前臺和后臺),并且scope都包含了api_name1,現(xiàn)在就會出現(xiàn)兩種情況:
前臺Client和后臺Client,都需要授權(quán)后訪問api/values接口:沒有問題。
前臺Client不需要授權(quán)后訪問,后臺Client需要授權(quán)后訪問:有問題,前臺Client沒辦法訪問了,因為api/values接口設(shè)置了Authorize()。
其實,說明白些,就是該如何讓 API 服務(wù)指定Client授權(quán)訪問?比如:[Authorize(ClientId = 'client_id_1')]。
2. 解決方案
沒有[Authorize(ClientId = 'client_id_1')]這種解決方式,不過可以使用[Authorize(Roles = 'admin')]。
授權(quán)中心的ResourceOwnerPasswordValidator代碼,修改如下:
{
? ?private readonly IUserService _userService;
? ?public ResourceOwnerPasswordValidator(IUserService userService)
? ?{
? ? ? ?_userService = userService;
? ?}
? ?public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
? ?{
? ? ? ?var user = await _userService.Login(context.UserName, context.Password);
? ? ? ?if (user != null)
? ? ? ?{
? ? ? ? ? ?var claims = new List<Claim>() { new Claim("role", "admin") }; //根據(jù) user 對象,設(shè)置不同的 role
? ? ? ? ? ?context.Result = new GrantValidationResult(user.UserId.ToString(), OidcConstants.AuthenticationMethods.Password, claims);
? ? ? ?}
? ?}
}
授權(quán)中心的startup配置,修改如下
builder.AddTemporarySigningCredential()
? ? ? ?//.AddInMemoryIdentityResources(Config.GetIdentityResources())
? ? ? ?.AddInMemoryApiResources(new List<ApiResource>
? ? ? ?{
? ? ? ? ? ?new ApiResource("api_name1", "api1"){ UserClaims = new List<string> {"role"}}, //增加 role claim
? ? ? ? ? ?new ApiResource("api_name2", "api2"){ UserClaims = new List<string> {"role"}}
? ? ? ?})
? ? ? ?.AddInMemoryClients(Config.GetClients());
API 服務(wù)接口,只需要配置如下:
[Route("api/values")]
[HttpGet]
public IActionResult Get()
{
? ?return Ok();
}
[Authorize(Roles = "admin")]
[Route("api/values2")]
[HttpGet]
public IActionResult Get2()
{
? ?return Ok();
}
[Authorize(Roles = "admin,normal")]
[Route("api/values3")]
[HttpGet]
public IActionResult Get3()
{
? ?return Ok();
}
需要注意的是,api/values接口雖然沒有設(shè)置具體的Roles,但每個Role都可以訪問。
相關(guān)文章:
IdentityServer4(OAuth2.0服務(wù))折騰筆記
IdentityServer4 實現(xiàn) OpenID Connect 和 OAuth 2.0
IdentityServer4 使用OpenID Connect添加用戶身份驗證
IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架學(xué)習(xí)保護(hù)API
原文地址:http://www.cnblogs.com/xishuai/p/identityserver4-apiresource-userclaim-role-authorize.html
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的IdentityServer4 指定角色授权(Authorize(Roles=amp;quot;adminamp;quot;))的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软中国Azure开源开发者(深圳)研讨
- 下一篇: Asp.Net Core 发布到IIS