AADSTS90102:'redirect_uri'值必须是有效的绝对Uri

时间:2017-10-10 07:11:37

标签: c# windows-services office365 azure-active-directory

我正在按照this页面上的说明操作。我自己创建了一个Windows服务,但我一直在向Azure AD请求访问令牌。 我设法获得了授权码,但是当我发布POST时,我收到了redirect_uri错误。这就是我的代码:

 var dictionary = new Dictionary<string, string>
            {
                { "resource", "https%3A%2F%2Foutlook.office365.com"},
                {"client_id","Application ID from azure AD portal" }, //-is this ok?
                {"client_secret","Object ID from azure AD portal" }, //-is this ok?
                {"grant_type","authorization_code" },
                {"redirect_uri",HttpUtility.UrlEncode("https://haw.trustteam.be/") },
                { "code","AQABAAIAAAAB..1AiAA"}
            };
            var content = new FormUrlEncodedContent(dictionary);

            string requestUrl = "https://login.windows.net/common/oauth2/token"; // also tried with login.microsoftonline.com
            using (HttpClient client = new HttpClient())
            {
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
                request.Content = content;

                using (HttpResponseMessage response = await client.SendAsync(request))
                {
                    string responseString = await response.Content.ReadAsStringAsync();

                    return response.Content.ToString();
                }
            }

我做错了什么?

1 个答案:

答案 0 :(得分:1)

FormUrlEncodedContent函数还有助于在HttpMessage正文中将数据作为url编码的键/值对发布。所以只需删除Observable函数:

HttpUtility.UrlEncode

此外,您还可以在蔚蓝广告应用的 var dictionary = new Dictionary<string, string> { { "resource", "https://outlook.office365.com"}, {"client_id","Application ID from azure AD portal" }, {"client_secret","Application key from azure portal" }, {"grant_type","authorization_code" }, {"redirect_uri","https://haw.trustteam.be/" }, { "code","AQABAAIAAAAB..1AiAA"} }; var content = new FormUrlEncodedContent(dictionary); 刀片中添加客户端密钥。请参阅this document