【问题标题】:Get user name and email using Microsoft.Identity.Client使用 Microsoft.Identity.Client 获取用户名和电子邮件
【发布时间】:2017-07-01 21:41:50
【问题描述】:

我正在使用这个包https://www.nuget.org/packages/Microsoft.Identity.Client 对用户进行身份验证,身份验证工作正常,问题是,我正在考虑使用登录后获得的令牌来获取用户名和电子邮件(因为我不仅需要访问收件箱、联系人和日历;而且使用电子邮件将用户链接到角色)。 问题是,当我得到令牌时,我得到一个长字符串作为 userId(我猜是加密的)。有什么方法可以使用这个包来获取电子邮件吗?

这是我拿回令牌的部分

public SessionTokenCache(string userId, HttpContextBase httpContext)
    {
        this.userId = userId;
        cacheId = userId + "_TokenCache";
        this.httpContext = httpContext;
        BeforeAccess = BeforeAccessNotification;
        AfterAccess = AfterAccessNotification;
        Load();
    }

这是我遵循的教程 https://dev.outlook.com/restapi/tutorial/dotnet

【问题讨论】:

    标签: c# asp.net-mvc email


    【解决方案1】:

    一旦你有了一个令牌,也许你可以使用 Graph API 来获取登录用户的详细信息?结果是 Json,可用于提取您想要的位。

            public static async Task<string> GetUserData(string token)
        {
            //get data from API
            HttpClient client = new HttpClient();
            HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me");
            message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token);
            HttpResponseMessage response = await client.SendAsync(message);
            string responseString = await response.Content.ReadAsStringAsync();
            if (response.IsSuccessStatusCode)
                return responseString;
            else
                return null;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      相关资源
      最近更新 更多