【问题标题】:Invalid Signature when generate bearer token生成不记名令牌时签名无效
【发布时间】:2019-08-11 19:58:05
【问题描述】:

我是 OAuth 新手,我使用 this 教程生成从客户端应用程序到目标应用程序的访问令牌。代码本身工作正常,但是当我在https://jwt.io/ 上解码时,我生成的访问令牌有invalid signature

这是教程中的代码

public class ServicePrincipal
    {
        /// <summary>
        /// The variables below are standard Azure AD terms from our various samples
        /// We set these in the Azure Portal for this app for security and to make it easy to change (you can reuse this code in other apps this way)
        /// You can name each of these what you want as long as you keep all of this straight
        /// </summary>
        static string authority = "";  // the AD Authority used for login.  For example: https://login.microsoftonline.com/myadnamehere.onmicrosoft.com 
        static string clientId = ""; // client app's client id
        static string clientSecret = ""; // client app's secret key
        static string resource = ""; // target app's App ID URL

        /// <summary>
        /// wrapper that passes the above variables
        /// </summary>
        /// <returns></returns>
        static public async Task<AuthenticationResult> GetS2SAccessTokenForProdMSAAsync()
        {
            return await GetS2SAccessToken(authority, resource, clientId, clientSecret);
        }

        static async Task<AuthenticationResult> GetS2SAccessToken(string authority, string resource, string clientId, string clientSecret)
        {
            var clientCredential = new ClientCredential(clientId, clientSecret);
            AuthenticationContext context = new AuthenticationContext(authority, false);
            AuthenticationResult authenticationResult = await context.AcquireTokenAsync(
                resource,  // the resource (app) we are going to access with the token
                clientCredential);  // the client credentials
            return authenticationResult;
        }
    }

我发现还有一段代码也可以生成访问令牌:

     AuthenticationContext authenticationContext =
   new AuthenticationContext({authority});

        ClientCredential clientCredential = new ClientCredential({client app id}, {client app secret});
        try
        {
            AuthenticationResult result =
              await authenticationContext.AcquireTokenAsync({target app's App ID URL},
                                                                clientCredential);
        }
        catch (Exception e)
        {
            return false;
        }

这两个代码都给了我无效的 1.0 版签名访问令牌

这里有两个问题:

  1. 我注意到当我解码访问令牌时,它显示"ver": "1.0"。这是否意味着它正在使用 OAuth1.0?因为我想使用 OAuth 2.0.. 为什么代码会生成创建 OAuth1.0 而不是 OAuth2.0 的令牌?

  2. 为什么会是无效签名?

【问题讨论】:

标签: azure asp.net-core oauth-2.0 azure-active-directory access-token


【解决方案1】:

我用你的代码尝试了相同的代码,得到了相同的情况invalid signature,但是当我将 jwt ALGORITHM 更改为 HS256 时,我得到了Signature Verified。

还有签名:

以及关于RRS256和HS256的区别:

RS256(带有 SHA-256 的 RSA 签名)是一种非对称算法,它使用公钥/私钥对:身份提供者拥有用于生成签名的私钥(秘密), JWT 的消费者获得一个公钥来验证签名。由于与私钥相反,公钥不需要保持安全,因此大多数身份提供者都可以让消费者轻松获取和使用(通常通过元数据 URL)。

另一方面,

HS256(带有 SHA-256 的 HMAC)是一种对称算法,只有一个(秘密)密钥在两方之间共享。由于生成签名和验证签名都使用同一个密钥,因此必须注意确保密钥不被泄露。

【讨论】:

  • 那么我们应该使用HS256吗?
  • 通常情况下,天蓝色广告会在发放令牌之前选择加密令牌的算法。
【解决方案2】:

我注意到,当我解码访问令牌时,它显示“ver”:“1.0”。这是否意味着它正在使用 OAuth1.0?因为我想使用 OAuth 2.0.. 为什么代码会生成创建 OAuth1.0 而不是 OAuth2.0 的令牌?

您使用的是 OAuth2.0 ,ver:"1.0" 表示 JWT 令牌由 Azure AD V1.0 Endpoint 颁发。

为什么会是无效签名?

API 需要检查 JWT 标头(属性 alg)指定的算法是否与 API 预期的匹配。 AAD 使用 RS256 ,所以你应该改为RS256:

通常的方法是从模数和指数构建,从https://login.microsoftonline.com/common/discovery/keys匹配kid和x5t从token中找到它们。任何使用在线工具(如https://play.golang.org/)来获取公钥都包含两个部分:n 和 e。您也可以使用x5c 值,点击here 获取示例。

【讨论】:

  • @WWpana , 你把你的客户端密码用来验证签名区吗?客户端密码是用于获取访问令牌的密码。客户端应用的客户端密码。
  • 两个区域都不需要放,下一个是用来发放token的。
  • 所以改写你的步骤:1)输入访问令牌并确保它是RS256当我完成这一步时,它已经显示无效签名2)然后在公钥部分 - 工具中没有发生任何事情。我错过了任何步骤吗?
【解决方案3】:

您是否将密钥发布到 jwt.io 的表单中?尝试使用授权标头中的令牌进行真正的休息调用。如果一切正常,而 jwt 却没有,也许它在他们身上。

【讨论】:

  • 我确实尝试使用访问令牌进行休息调用,但它一直给我 401 错误而没有达到我的任何断点。所以这就是为什么我怀疑访问令牌有问题。我还使用jsonwebtoken.io 来验证令牌,它给了我相同的结果 - Invalid - Token Signature has failed
  • 再次调用 rest,运行 fiddler 或 Insomnia 并发布跟踪。让我们先排除调用然后移动到令牌
  • 会的。同时,我编辑了问题,发现解码后的令牌显示ver: 1.0.. 这是否意味着它给了我一个带有 OAuth1.0 的令牌?
  • 是的。除非有理由使用 1,否则您应该使用 oAuth2.0。如果您使用 .net,则应该使用 OWIN。遵循本指南。 bitoftech.net/2014/06/01/…
  • 我正在使用 asp.net core
猜你喜欢
  • 1970-01-01
  • 2016-12-20
  • 2017-03-20
  • 1970-01-01
  • 2022-12-05
  • 2021-06-21
  • 2021-11-28
  • 1970-01-01
  • 2019-07-14
相关资源
最近更新 更多