繁体   English   中英

生成承载令牌时签名无效

[英]Invalid Signature when generate bearer token

我是OAuth的新手,我使用了教程来生成从客户端应用到目标应用的访问令牌。 代码本身可以正常工作,但是当我在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. 为什么会是无效签名?

在此处输入图片说明

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

在此处输入图片说明

以及签名:

在此处输入图片说明

以及有关RRS256和HS256的区别:

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

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

您是否将密钥发布到jwt.io的表单中? 尝试使用授权标头中的令牌进行真正的休息呼叫。 如果一切正常,并且jwt无效,则可能是在它们上面。

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

您使用的是OAuth2.0,版本ver:"1.0"表示JWT令牌是由Azure AD V1.0端点发行的。

为什么会是无效签名?

API需要检查JWT标头(property alg)指定的算法是否与API期望的算法匹配。 AAD使用RS256 ,因此您应该更改为RS256

在此处输入图片说明

通常的方法是从模数和指数中构建,然后从https://login.microsoftonline.com/common/discovery/keys找到它们,并从令牌中匹配kid和x5t。 任何使用诸如https://play.golang.org/之类的在线工具来获取公钥的操作都包含两个部分:n和e。 您也可以使用x5c值,请单击此处获取示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM