首页 > 解决方案 > 在 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;
        }         
    }

标签: asp.net-mvcopenid-connectonelogin

解决方案


您需要在此消息中再次发送 redirect_uri - 这是授权代码流的安全功能。

还值得使用诸如 Fiddler 之类的工具来跟踪消息,以确保通过网络发送的消息是您所期望的。

请参阅我的消息中的第 4 步和第 8 步,以便进行比较。


推荐阅读