【问题标题】:Azure AD token service does't response refresh_token and id_tokenAzure AD 令牌服务不响应 refresh_token 和 id_token
【发布时间】:2017-01-12 00:15:18
【问题描述】:

我正在尝试使用 Outlook REST API。我必须通过 Azure AD 进行身份验证,对此我有一个小问题。当我将authorization code 交换为access token 时,https://login.microsoftonline.com/common/oauth2/v2.0/token 的响应不包含我需要的 refresh_token 和 id_token。我的代码发送请求是

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("https://login.microsoftonline.com/common/oauth2/v2.0/token");
req.Method = "POST";
req.ServicePoint.Expect100Continue = false;
req.UserAgent = "Example/1.0";
req.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter sw = new StreamWriter(req.GetRequestStream()))
{
    string data = "";
    data += "grant_type=authorization_code";
    data += "&code=" + Request.QueryString("code");
    data += "&scope=" + HttpUtility.UrlEncode(string.Join(" ", scopes));
    data += "&redirect_uri=" + HttpUtility.UrlEncode(redirectUri);
    data += "&client_id=" + appId;
    data += "&client_secret=" + appPassword;
    sw.Write(data);
}
HttpWebResponse res = req.GetResponse();
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
    Response.ContentType = "application/json";
    Response.Write(sr.ReadToEnd());
    Response.End();
}

此代码的响应示例

{
    "token_type": "Bearer",
    "scope": "https://outlook.office.com/Calendars.Read https://outlook.office.com/Calendars.ReadWrite https://outlook.office.com/Mail.Read",
    "expires_in": 3600,
    "ext_expires_in": 0,
    "access_token": "EwAYA+l3B/Qk ... IpfA0C"
}

我不知道我在做什么与https://oauthplay.azurewebsites.net 不同,因为那里的响应包含所有属性。

【问题讨论】:

  • 对于刷新令牌,您必须在请求授权代码时询问 Offline_Access 范围,对于 id_token,我猜您需要 Profile 范围。 id_token 基本上用于特定应用程序,只是为了验证令牌仅被特定应用程序使用,并且没有其他应用程序代表其他应用程序使用令牌。因此,请尝试请求可能为您提供刷新令牌的 offline_access 范围。
  • 谢谢。有用。你能发布作为答案吗?我会标记的。

标签: c# azure oauth-2.0 azure-active-directory


【解决方案1】:

对于刷新令牌,您必须在请求授权代码时请求Offline_Access 范围,对于 id_token,我猜您需要 Profile 范围。 id_token 基本上用于特定应用程序,只是为了验证令牌仅被特定应用程序使用,并且没有其他应用程序代表其他应用程序使用令牌。因此,请尝试请求offline_access 范围,它可能会为您提供刷新令牌。

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2020-09-09
    • 2021-08-17
    相关资源
    最近更新 更多