问题描述
我有一个 WPF 应用程序(.net 462 和 API .net5.0)。
我已经在 api 和这项工作上设置了 openid,权限工作,但在 WPF 应用程序上,我必须检查应用程序元素的权限(菜单访问、按钮访问)。
获取令牌工作,但我不知道如何在 openid 时验证范围
关键时钟配置:
- Realm => 演示测试
- 客户端 => 演示测试客户端
- 客户角色 => DemoRole
- 授权范围 => demo:read
- 相关权限 => 权限演示:阅读
- 角色相关政策 => DemoRole - 正面
我创建了两个用户“user-test-ok”和“user-test-ko”,“user-test-ok”具有客户端角色“DemoRole”。
我对用户进行了自省测试,以验证用户是否具有“demo:read”范围,但这不起作用。
我不想使用 keycloak API,我想使用 openid 系统,以便其他 OAuth2.0 系统可能更改 keycloak。
这是我尝试检查授权范围的代码:
var requestUri = new Uri($"https://localhost:8443/auth/realms/{realm}/protocol/openid-connect/token/introspect");
using (var client = new HttpClient())
{
var req = new HttpRequestMessage(HttpMethod.Post,requestUri);
req.Headers.Add("cache-control","no-cache");
req.Headers.Add("accept","application/x-www-form-urlencoded");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",accesstoken);
req.Content = new FormUrlEncodedContent(new Dictionary<string,string>
{
{ "token_type_hint","requesting_party_token" },{ "token",tokenResult.Accesstoken },{ "client_id",clientId },{ "client_secret",clientSecret },{ "scope","test:read" },});
var response = client.SendAsync(req).Result;
if (!response.IsSuccessstatusCode)
{
throw new Exception();
}
var responseString = response.Content.ReadAsstringAsync().Result;
}
你知道怎么做吗?