【问题标题】:how to generate token from azure AD app client id?如何从 azure AD 应用程序客户端 ID 生成令牌?
【发布时间】:2018-10-05 09:49:41
【问题描述】:

我有one application which is register into azure AD. 我有client id with me and secret key is inside the key vault.

如何使用控制台应用访问安全的 Azure AD 注册 API?

我猜我是need a bearer token for it how to generate it?

我搜索并得到类似下面的代码 -

var authority = "https://login.microsoftonline.com/your-aad-tenant-id/oauth2/token";
var context = new AuthenticationContext(authority);
var resource = "https://some-resource-you-want-access-to";

var clientCredentials = new ClientCredential(clientId, clientSecret);

var result = await context.AcquireTokenAsync(resource, clientCredentials);  

【问题讨论】:

  • 此控制台应用程序仅用于测试目的吗?还是会在生产场景中继续使用此 API 的真实客户端?我问这个是因为如果它是一个真正的客户端,你应该在 Azure AD 中将它注册为一个单独的应用程序,而不是尝试使用 API 本身的 clientID 和密钥。我可以根据具体情况在答案中为你提供更具体的指导它是..
  • 这是真实的客户端应用生产场景。在这种情况下需要做什么?如果您在此处指出某些内容,将会有很大帮助
  • 控制台应用程序是否在客户端计算机上运行?你意识到客户端的秘密会被有效地公开吗?

标签: c# azure azure-active-directory


【解决方案1】:

您可以尝试下面的代码来生成令牌,在我的示例中,我为https://graph.microsoft.com 生成令牌。

string graphResourceId = "https://graph.microsoft.com/";
string authority = "https://login.microsoftonline.com/your-aad-tenant-id/oauth2/token";
string tenantId = "your-aad-tenant-id";
string clientId = "your-clientid";
string secret = "your-secret";
authority = String.Format(authority, tenantId);
AuthenticationContext authContext = new AuthenticationContext(authority);
var accessToken = authContext.AcquireTokenAsync(graphResourceId, new ClientCredential(clientId, secret)).Result.AccessToken;

【讨论】:

  • 但是没有密钥?我认为他们已将其添加到密钥库中,如果是这样,如何从密钥库中使用它?
  • @Neo 你可以参考这个post 了解他们的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 2020-03-28
  • 1970-01-01
  • 2023-03-29
  • 2023-01-11
  • 2022-10-06
相关资源
最近更新 更多