c# - Azure AD token 服务不响应refresh_token 和id_token

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

我正在尝试使用 Outlook REST API。我必须通过 Azure AD 进行身份验证,但我遇到了一个小问题。当我将授权代码交换为https://login.microsoftonline.com/common/oauth2/v2.0/token访问 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 不同因为响应包含所有属性。

最佳答案

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

关于c# - Azure AD token 服务不响应refresh_token 和id_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316806/

相关文章:

C# - 在文件中查找一行(正则表达式)并根据另一个正则表达式获取完整的文本 block

c# - 带有 Xamarin 的 Android Things IGpioCallback 问题

azure - 尝试使用 azure 保管库证书 key 解密时出错 : "Operation returned an invalid status code ' Forbidden'"

winforms - 是否可以将 power bi 嵌入到桌面应用程序中?

oauth-2.0 - 通过 oauth (google) 获取用户所属的所有组

facebook-graph-api - 何时应使用服务器端与客户端 Facebook 身份验证流程?

c# - 如何执行行锁?

c# - 刷新后重新抛出 Entity Framework OptimisticConcurrencyException

wordpress - 容器 Web 应用程序 - Wordpress Https

facebook - Xamarin Forms + IdentityServer + 外部供应商 (Facebook/Google)