检索应用程序服务的访问令牌

时间:2018-08-01 16:32:43

标签: c# azure azure-active-directory botframework

我目前拥有在Visual Studio中创建的MS团队机器人。我需要一种方法来检索MS Teams中Bot用户的信息,例如他们的电子邮件地址,全名等。该Bot作为应用程序服务托管在Azure中。我已点击此链接https://docs.microsoft.com/en-us/azure/app-service/app-service-web-tutorial-auth-aad,并在我的应用程序服务上配置了Azure Active Directory身份验证。我有以下代码尝试从Azure检索访问令牌,然后将其用于从https://graph.microsoft.com/v1.0/me获取信息。

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;
}

我已经从其他地方获取了此代码,但是我遇到的问题是我看不到Azure Active Directory应用程序的客户端机密,并且我不知道应该是什么“授权”。这是我第一次使用这样的工具,所以希望在此方面提出任何建议或提示。


static string authority = "https://login.microsoftonline.com/common";
static string clientId = "my_azure-ad_app_id";
static string resource = "https://<app_name>.azurewebsites.net/";
static string clientSecret = "azure-ad_app_secret";

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;
}

这是到目前为止我要检索访问令牌的代码,但是它不返回任何内容。

2 个答案:

答案 0 :(得分:1)

可以通过在其“应用程序注册”刀片中生成一个秘密(或密钥)来获取该秘密。您应该转到Azure Active Directory->应用程序注册->查找应用程序->密钥->输入名称,选择持续时间并保存。 现在,您应该拥有密钥了。

权限基本上是“我们将对其进行身份验证的身份提供者”。 因此,如果您的应用仅用于组织中, 您需要指定该AAD租户。 在这种情况下,权限为:

https://login.microsoftonline.com/your-tenant.onmicrosoft.com

your-tenant.onmicrosoft.com替换为您在AAD中拥有的任何经过验证的域名(始终包含此格式的一个),或使用您的AAD租户ID(您可以从AAD->“属性”中找到它)。

如果您的应用程序应支持任何组织(即,它是多租户应用程序), 那么权威永远是:

https://login.microsoftonline.com/common

答案 1 :(得分:0)

添加到Junnas答案。您可以尝试这个reference doc

客户ID:

enter image description here

客户机密:

enter image description here

注意:“客户端机密”仅在创建时可见。在那之后,它将不再出现。因此,请确保您第一次进行复制