在MS Bot Framework机器人中获取Azure Active Directory用户的数据

我在Azure帐户上部署了一个用Node.js编写的Cortana机器人。 僵尸程序使用Cortana通道并打开“Connected Services”(所以我可以使用Azure Active Directory中的帐户login僵尸工具)。 login工作正常。

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

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

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

虽然我没有关于如何使用Node来做这个示例,也许C#中的解决scheme将有所帮助?

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

entities集合中,Cortana放置一个authorization条目,其中包含由连接服务的身份validationstream程产生的令牌。 如果您使用连接的服务打击Microsoft服务(也就是login到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 { } } 

希望有所帮助! 如果你想玩你从图中的各个租户得到的东西, graphics浏览器是一个很好的方法来做到这一点。