AADSTS90102: 'redirect_uri' value must be a valid absolute Uri

10,286

FormUrlEncodedContent function also help posting data in the HttpMessage body as url-encoded key/value pairs. So just remove the HttpUtility.UrlEncode function:

            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);

In addition , you can add client secret in Keys blade of your azure ad application . Please refer to this document .

Share:
10,286

Related videos on Youtube

Vlad Enache
Author by

Vlad Enache

Updated on June 04, 2022

Comments

  • Vlad Enache
    Vlad Enache almost 2 years

    I'm following the instructions from this page. I've created myself a windows service and I'm stuck at requesting access token from Azure AD. I managed to get an authorization code but I get the redirect_uri error when I POST. This is what my code looks like:

     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();
                    }
                }
    

    What am I doing wrong?