asp.net-mvc - 在 One Login 身份验证授权类型第 2 部分请求中获取 401 未经授权的错误
问题描述
我正在使用 MVC 框架向 OneLogin API 发出 POST 请求以获取 JWT。我在下面的代码中的 PostAsync 调用中收到了 401 未经授权的消息。
错误如下所示
StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Cache-Control: no-store, no-cache
Date: Wed, 17 Jun 2020 04:21:23 GMT
Set-Cookie: ol_oidc_canary_30=false; path=/; domain=.onelogin.com
X-Powered-By: Express
Content-Length: 77
Content-Type: application/json; charset=utf-8
}
我是否缺少任何参数。我在 One login dev 帐户上注册了我的本地主机。我需要更新或更改任何设置吗?
public async Task<OidcTokenResponse> ProcessToken(string code, string clientSec)
{
string authorityToken = OneLoginAuthorityToken;
var formData = new System.Net.Http.FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("code", code),
new KeyValuePair<string, string>("client_id", OneLoginClientID),
new KeyValuePair<string, string>("client_secret", clientSec),
new KeyValuePair<string, string>("grant_type", "authorization_code"),
});
using (var client = new System.Net.Http.HttpClient())
{
const SslProtocols _Tls12 = (SslProtocols)0x00000C00;
const System.Net.SecurityProtocolType Tls12 = (System.Net.SecurityProtocolType)_Tls12;
System.Net.ServicePointManager.SecurityProtocol = Tls12;
var res = await client.PostAsync(authorityToken, formData);
var json = await res.Content.ReadAsStringAsync();
var tokenReponse = Newtonsoft.Json.JsonConvert.DeserializeObject<OidcTokenResponse>(json);
return tokenReponse;
}
}
解决方案
您需要在此消息中再次发送 redirect_uri - 这是授权代码流的安全功能。
还值得使用诸如 Fiddler 之类的工具来跟踪消息,以确保通过网络发送的消息是您所期望的。
请参阅我的消息中的第 4 步和第 8 步,以便进行比较。
推荐阅读
- python - 在列表中按升序打印所有序列的功能
- php - 如何限制直接链接的删除访问?
- virtual-machine - Google Cloud Platform - 捕获并记录错误并自动终止 VM
- python-3.x - Numpy以绝对值查找特定距离内的点数
- awk - 如何在文件中的字符串之后查找子字符串并在 shell scipting 中使用 sed/awk 增加该数字?
- database - 为什么我不能从 dolphindb 中的另一个连接获取变量?
- java - 更新表中的数据时清除所选行
- collections - 使用嵌套地图
- pdf - 你可以在 Flutter 中将屏幕的数据转换为可下载的 pdf 报告吗?
- c# - 如何在谓词参数上使用 NSubstitute 返回?