【发布时间】:2022-01-15 02:05:29
【问题描述】:
我无法从 API 中获取用户身份信息。 我的项目由一个独立的 WASM 应用、IDP 和 WebApi 组成。
我已经完成了所有设置并且它可以工作,但我需要的是来自 Blazor 客户端的调用,以从 api 获取一些数据。然后,API 使用用户的电子邮件地址来识别他们并为他们获取数据。
我查看了类似的问题,但解决方案不适用于我的项目。
[HttpGet("GetData")]
public async Task<IActionResult> GetData()
{
string test = User.Identity.Name; // returns null
string username = "myuser@users.com";
List<string> data= new List<string>();
data= (await _dataRepository.GetData(username)).ToList();
if (data.Count > 0)
{
return Ok(data);
}
else
{
return NoContent();
}
}
那么我在哪里设置用户名有没有办法获取通过请求的用户的电子邮件?
已编辑 访问令牌:
{
"alg": "RS256",
"kid": "2D49329C75FC43C78590AF6F6A0EFDB2",
"typ": "at+jwt"
}
{
"nbf": 1639243158,
"exp": 1639246758,
"iss": "https://localhost:5000",
"aud": "https://localhost:5000/resources",
"client_id": "ATS",
"sub": "4892725f-f6da-4a28-827a-ce666bb6f098",
"auth_time": 1638729064,
"idp": "local",
"jti": "53CB2F8FCB2EB34E3501E2C210B59B5D",
"sid": "8463E4AA74D7369C1176249ED8FA46B1",
"iat": 1639243158,
"scope": [
"openid",
"profile",
"MY_API"
"email"
],
"amr": [
"pwd"
]
}
【问题讨论】:
-
你的api使用的是什么认证方式?
-
Oidc 身份验证
-
请发布您配置身份验证和授权的启动类的副本。同时发布示例 ID/访问令牌。
-
// 授权策略 var requireAuthenticationUserPolicy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() .Build(); builder.Services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) .AddIdentityServerAuthentication(options => { options.Authority = "localhost:5000"; options.ApiName = "MY_API"; options.LegacyAudienceValidation = true; });
标签: asp.net-web-api blazor identityserver4 identity webapi