【问题标题】:Outlook Exchange Office 365: How to get email address from access token?Outlook Exchange Office 365:如何从访问令牌中获取电子邮件地址?
【发布时间】:2016-09-13 01:21:43
【问题描述】:

目前,我正在关注这篇文章https://dev.outlook.com/restapi/tutorial/dotnet。一切正常,但我无法从访问令牌中检索用户的电子邮件地址。

 private string GetEmailFromIdToken(string token) {
        // JWT is made of three parts, separated by a '.' 
        // First part is the header 
        // Second part is the token 
        // Third part is the signature 
        string[] tokenParts = token.Split('.');
        if (tokenParts.Length < 3) {
            // Invalid token, return empty
        }
        // Token content is in the second part, in urlsafe base64
        string encodedToken = tokenParts[1];
        // Convert from urlsafe and add padding if needed
        int leftovers = encodedToken.Length % 4;
        if (leftovers == 2) {
            encodedToken += "==";
        } else if (leftovers == 3) {
            encodedToken += "=";
        }
        encodedToken = encodedToken.Replace('-', '+').Replace('_', '/');
        // Decode the string
        var base64EncodedBytes = System.Convert.FromBase64String(encodedToken);
        string decodedToken = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        // Load the decoded JSON into a dynamic object
        dynamic jwt = Newtonsoft.Json.JsonConvert.DeserializeObject(decodedToken);
        // User's email is in the preferred_username field
        return jwt.preferred_username;
    }

在一些文章中人们说要添加“openid”

 private static string[] scopes = { "openid", "https://outlook.office.com/mail.read",
                                   "https://outlook.office.com/calendars.read" };

在范围内。但是它仍然不起作用。它甚至没有成功登录,抛出异常。 当我不添加“openid”时,我会成功登录,但是preferred_username 为空。

【问题讨论】:

    标签: c# outlook oauth-2.0 office365 access-token


    【解决方案1】:

    我见过一些preferred_username 声明不是 SMTP 地址的情况,因此它实际上不是获取用户电子邮件地址的最佳方式。我建议不要解析 ID 令牌,只需对 GET /Me 进行 API 调用,其中应包含 EmailAddress 属性。

    我正在修改 dev.outlook.com 上的教程来做到这一点。

    【讨论】:

      【解决方案2】:

      这是一个很好的收获!

      您需要包含“配置文件”范围才能读取首选用户名,

      private static string[] scopes = { "profile",
                                             "https://outlook.office.com/mail.read",
                                             "https://outlook.office.com/calendars.read" };
      

      或者,尝试使用“contacts.read”范围

      private static string[] scopes = { "https://outlook.office.com/contacts.read",
                                             "https://outlook.office.com/mail.read",
                                             "https://outlook.office.com/calendars.read" };
      

      https://github.com/OfficeDev/microsoft-graph-docs/blob/master/content/authorization/permission_scopes.md 中了解有关权限范围的更多信息

      【讨论】:

      • 感谢您的快速回复,是的,现在它正在发送电子邮件,但令牌现在为空。如何获取令牌和电子邮件地址。 +1 .. 再次感谢您的回复
      • 你指的是哪个令牌?
      • var authResult = await authContext.AcquireTokenByAuthorizationCodeAsync( authCode, onSuccessRedirectUri, credential, scopes); var outlookToken = new OutlookToken(); // 将令牌保存在会话中 outlookToken.Token = authResult.Token;
      • 我们也可以在范围内添加“email”添加
      猜你喜欢
      • 2018-10-27
      • 2018-09-05
      • 1970-01-01
      • 2017-05-07
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 1970-01-01
      相关资源
      最近更新 更多