0

我正在使用 Microsoft Graph API 对我的 Web 应用程序的用户进行身份验证。对于 SO,我已经在Registration Portal中注册了一个测试应用程序。此门户为我们提供应用程序 ID 和应用程序密码。我已经定义 了重定向 URL,它接受代码并返回访问令牌。

 public async Task<AuthToken> ExchangeCodeForAccessToken(string code)
    {
        MicrosoftGraphTokenResponse tokenResponse = null;
        using (HttpClient client = new HttpClient())
        {
            var postFormParameters = new List<KeyValuePair<string, string>>()
            {
                    new KeyValuePair<string, string>("client_id", _msGraphpParameter.AppId),
                    new KeyValuePair<string, string>("redirect_uri", _msGraphpParameter.RedirectUrl),
                    new KeyValuePair<string, string>("code", code),
                    new KeyValuePair<string, string>("grant_type", "authorization_code"),
                    new KeyValuePair<string, string>("scope", _msGraphpParameter.Scopes)
            };

            if (_msGraphpParameter.AppSecret != null)
            {
                // Secret isn't needed for client apps
                postFormParameters.Add(new KeyValuePair<string, string>("client_secret", _msGraphpParameter.AppSecret));
            }

            var formContent = new FormUrlEncodedContent(postFormParameters);
            client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", TokenRefreshContentType);

            HttpResponseMessage response = await client.PostAsync(MsGraphTokenRefreshUrl, formContent);
            string responseString = await response.Content.ReadAsStringAsync();
            tokenResponse = JsonConvert.DeserializeObject<MicrosoftGraphTokenResponse>(responseString);

        }
        return TransformMSGraphTokenResponseIntoAuthProperties(tokenResponse);
    }

我的问题有点奇怪。我可以毫无问题地访问令牌,这意味着我已经进行了正确的设置。但是,经过几次调用(2),Graph API 没有返回任何内容,因为未找到登录 live dot com服务器。

我已经尝试完全清除 cookie、dns 缓存,但它没有工作,但是如果我重新启动笔记本电脑并拨打电话,那么它似乎开始工作了。几次通话后,仍然没有找到 Login.live.com 服务器。

这是快照在此处输入图像描述

请让我知道你的想法。谢谢

4

0 回答 0