OpenID Connect 身份验证成功。怎么办?
OpenID Connect Authentication Successful. Now what?
我正在用 C# 编写 windows 服务,需要使用 API 进行身份验证并进行一些调用。我可以通过我正在与之交谈的 API 成功进行身份验证,但我似乎无法弄清楚如何使用响应。响应如下所示:
{"access_token":"Es-Zjs_LI0tcXyLe3aEfgKPNLHN7CwyUhTss-cTld1A","expires_in":1800,"token_type":"Bearer","scope":"example","auth_state":1,"company":"examplecompany"}
如果需要,我可以从该字符串中获取访问令牌,但无论我如何将其传递给请求,我都会收到 401 错误。这是我当前迭代的样子:
string results = "";
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",token);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://example.ca/endpoint"),
//Headers =
//{
// { "authorization", "Bearer"},
//},
};
try
{
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
results = body;
}
}
catch (Exception ex)
{
results = "ERROR: " + ex.Message;
}
return results;
其中“token”是本例中的字符串“Es-Zjs_LI0tcXyLe3aEfgKPNLHN7CwyUhTss-cTld1A”。我之前曾尝试将 access_token 值作为字符串拼接到中间注释掉的部分中的“Bearer”字符串。我究竟做错了什么?我需要从响应中生成 JwtSecurityToken 吗?
AuthenticationResult authResult = await daemonClient.AcquireTokenForClient(new[] { MSGraphScope })
.ExecuteAsync();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
我用过authResult.AccessToken
。不确定它是否适用于您的场景。 return 类型在我的例子中是 Microsoft.Identity.Client.AuthenticationResult
类型,当我检索我正在使用的图 API 的令牌时。
请注意,您收到的令牌(“Es-Zjs_LI0tcXyLe3aEfgKPNLHN7CwyUhTss-cTld1A”)是参考令牌,而不是 JWT 令牌。确保您的 API 接受该类型的令牌。
为了在生产中有效地使用令牌,我会考虑使用 IdentityModel library and especially the Worker application 帮助器中的各种帮助器方法。
虽然我知道这在很大程度上取决于您尝试连接的 API 情况,但对我来说,解决方案是使用此方法传递身份验证令牌:
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token);
我正在用 C# 编写 windows 服务,需要使用 API 进行身份验证并进行一些调用。我可以通过我正在与之交谈的 API 成功进行身份验证,但我似乎无法弄清楚如何使用响应。响应如下所示:
{"access_token":"Es-Zjs_LI0tcXyLe3aEfgKPNLHN7CwyUhTss-cTld1A","expires_in":1800,"token_type":"Bearer","scope":"example","auth_state":1,"company":"examplecompany"}
如果需要,我可以从该字符串中获取访问令牌,但无论我如何将其传递给请求,我都会收到 401 错误。这是我当前迭代的样子:
string results = "";
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer",token);
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://example.ca/endpoint"),
//Headers =
//{
// { "authorization", "Bearer"},
//},
};
try
{
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
results = body;
}
}
catch (Exception ex)
{
results = "ERROR: " + ex.Message;
}
return results;
其中“token”是本例中的字符串“Es-Zjs_LI0tcXyLe3aEfgKPNLHN7CwyUhTss-cTld1A”。我之前曾尝试将 access_token 值作为字符串拼接到中间注释掉的部分中的“Bearer”字符串。我究竟做错了什么?我需要从响应中生成 JwtSecurityToken 吗?
AuthenticationResult authResult = await daemonClient.AcquireTokenForClient(new[] { MSGraphScope })
.ExecuteAsync();
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
我用过authResult.AccessToken
。不确定它是否适用于您的场景。 return 类型在我的例子中是 Microsoft.Identity.Client.AuthenticationResult
类型,当我检索我正在使用的图 API 的令牌时。
请注意,您收到的令牌(“Es-Zjs_LI0tcXyLe3aEfgKPNLHN7CwyUhTss-cTld1A”)是参考令牌,而不是 JWT 令牌。确保您的 API 接受该类型的令牌。
为了在生产中有效地使用令牌,我会考虑使用 IdentityModel library and especially the Worker application 帮助器中的各种帮助器方法。
虽然我知道这在很大程度上取决于您尝试连接的 API 情况,但对我来说,解决方案是使用此方法传递身份验证令牌:
request.Headers.TryAddWithoutValidation("Authorization", "Bearer " + token);