2017-10-05 52 views
1

我在我的Azure帐户中部署了一个用Node.js编写的Cortana bot。僵尸程序使用Cortana渠道并启用了“连接服务”(因此我可以使用Azure Active Directory中的帐户登录僵尸工具)。登录工作正常。在MS Bot框架中获取Azure Active Directory用户的数据bot

我的问题是,现在我不知道如何获取我的机器人中登录用户(例如电子邮件地址)的相关信息。

我猜我需要从会话数据中获取一些令牌,然后向用户的数据发送一个请求Azure的API请求?

Node.js有这个botauth的例子,但运行它的指令已过时。

+0

关于node.js的例子,你能澄清一下在这个例子中过时了吗? –

+0

您是否与Azure AD一起工作?尼斯。我似乎无法找到正确的东西来填充连接服务屏幕的正确框中 - 任何机会,你可以张贴blured截图或类似的东西,从Azure AD应用程序注册的哪些设置去了Cortana Connected服务屏幕? –

回答

0

虽然我没有关于如何使用Node做到这一点的示例,也许C#中的解决方案将有所帮助?

您这样做的方式是检查Activity对象到达控制器的entities集合。由于这是的一部分,每请求bot,您可以随时做到这一点。

entities集合中,Cortana放入authorization条目,其中包含由连接服务的身份验证流程产生的令牌。如果您使用连接服务来访问Microsoft服务(即登录到Live,MSA等),则可以绕过此令牌并从Microsoft Graph请求关于该用户的信息。

在C#中,它看起来像这样:

// auth token from cortana's connected service stored as Entity object of type 'authorization' with child property 'token' holding the actual value 
var token = activity.Entities.Single(e => e.Type == @"authorization").Properties.Value<string>(@"token"); 
using (var client = new HttpClient()) 
{ 
    // The token can be used to query the Graph, but only because we know that it is from MS Identity. We couldn't query the Graph like this if we were using our own Identity provider (eg: Contoso Identity) 
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(@"Bearer", token); 
    try 
    { 
     var response = await client.GetAsync(@"https://graph.microsoft.com/v1.0/me").ConfigureAwait(false); 
     if (response.IsSuccessStatusCode) 
     { 
      var resultString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); 

      /* Response from /me looks like: 
       { 
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", 
        "givenName": "Brandon", 
        "surname": "H", 
        "displayName": "Brandon H", 
        "id": "<unique user id!>", 
        "userPrincipalName": "<E-MAIL ADDRESS!!>", 
        "businessPhones": [], 
        "jobTitle": null, 
        "mail": null, 
        "mobilePhone": null, 
        "officeLocation": null, 
        "preferredLanguage": null 
       } 
      */ 
     } 
    } 
    catch { } 
} 

希望帮助!如果你想玩你从图表的各个租户得到的回报,Graph Explorer是一个很好的方法来做到这一点。

相关问题