【发布时间】: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 版签名访问令牌
这里有两个问题:
我注意到当我解码访问令牌时,它显示
"ver": "1.0"。这是否意味着它正在使用 OAuth1.0?因为我想使用 OAuth 2.0.. 为什么代码会生成创建 OAuth1.0 而不是 OAuth2.0 的令牌?为什么会是无效签名?
【问题讨论】:
-
是否设置了权限、clientId等?
-
@VolodymyrBilyachat 是的..
标签: azure asp.net-core oauth-2.0 azure-active-directory access-token