生成不记名令牌时签名无效

sup*_*nja 1 azure access-token oauth-2.0 azure-active-directory asp.net-core

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

这是教程中的代码

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

     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;
        }
Run Code Online (Sandbox Code Playgroud)

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

这里有两个问题:

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

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

在此输入图像描述

Sun*_*Sun 5

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

在此输入图像描述

以及签名:

在此输入图像描述

RRS256和HS256的区别:

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

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

  • 通常,azure ad会在发布令牌之前选择加密令牌的算法。 (2认同)