[英]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版的無效簽名訪問令牌
這里有兩個問題:
我注意到,當我解碼訪問令牌時,它顯示為"ver": "1.0"
。 這是否意味着它正在使用OAuth1.0? 因為我想使用OAuth 2.0。為什么代碼會生成創建OAuth1.0而不是OAuth2.0的令牌?
為什么會是無效簽名?
我嘗試了與您的代碼相同的代碼,但遇到相同情況的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.