AADSTS90102:'redirect_uri'的值必须是绝对有效的Uri
问题描述:
我遵循this页面的指示。我已经创建了自己的Windows服务,并且我一直在向Azure AD请求访问令牌。 我设法获得一个授权码,但是当我POST时,我得到了redirect_uri错误。这是我的代码看起来像:AADSTS90102:'redirect_uri'的值必须是绝对有效的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();
}
}
我在做什么错?
答
FormUrlEncodedContent函数还可以帮助在HttpMessage正文中发布数据作为url编码的键/值对。所以,只是删除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);
此外,您可以在Keys
刀片的蔚蓝广告应用程序的添加客户端密钥。请参阅this document。