2017-10-05 50 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是一個很好的方法來做到這一點。

相關問題