How to fix "AADSTS90102: 'redirect_uri' value must be a valid absolute Uri." error in Microsoft Graph
E

5

6

Following this procedure: https://learn.microsoft.com/en-us/graph/auth-v2-user

I'm trying to get a refresh token from this microsoft end point: https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/authorize

Using PostAsync method from System.Net.Http.HttpClient class from nuget (asp.net core 2.2) library, I'm able to get a response back with this error: "AADSTS90102: 'redirect_uri' value must be a valid absolute Uri.": https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKmltX1ywKmMva3/LhP5kYf.png

I tried to set some redirect url in the Azure Portal including: https://automation.legroupeti.com/Configurations/GetRefreshToken/ https://automation.legroupeti.com/Configurations/GetRefreshToken https://automation.legroupeti.com/ https://automation.legroupeti.com

The post request is the following (Using PostAsync method from System.Net.Http.HttpClient class from nuget (asp.net core 2.2)): https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKmltX1ywKmMva3/PI4mo8Y.png

Here are the configured redirect urls form the registred application in the Azure Portal: https://static.mcmap.net/file/mcmap/ZG-AbGLDKwfpKmltX1ywKmMva3/aqYDJhn.png

I expect a valid response from the endpoint. How do I configure the redirect_uri to be valid?

EDIT 1

I fixed the redirect_uri parameter.

Emilio answered 4/9, 2019 at 13:32 Comment(0)
I
2

You seem to be mixing the authorize and token endpoints.

If you want the user to authenticate, you have to redirect the user to that URL, not send a POST request to it. After the user returns to your app, you need to exchange the authorisation code for tokens. Documentation: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-web-app-call-api-overview

If you want a token just for your app without user authentication, you need to call the token endpoint. Documentation: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-daemon-overview

Ibson answered 4/9, 2019 at 13:59 Comment(3)
I'm trying to use the login.microsoftonline.com{tenantId}/oauth2/v2.0/authorize endpoint to get an authorization code. This is why I'm doing a post resquest at login.microsoftonline.com{tenantId}/oauth2/v2.0/authorize. With the following parameters: client_id, response_type, redirect_uri, response_mode, client_secret, scope and state. As describe here: learn.microsoft.com/en-us/graph/auth-v2-user, the response should be a page where the user need to consent, then it should redirect to the redirect_uri after the consent.Emilio
You can't get an authorization code with a post request. You need to redirect a user browser there.Ibson
Redirect the user instead of doing a post to the url did fix the issue.Emilio
S
7

I was getting this error and for me the issue was that I was encoding the redirect_uri value in the post request to the /oauth2/v2.0/token endpoint. Notice how redirect_uri is not encoded in this request.

POST /{Tenant ID}/oauth2/v2.0/token HTTP/1.1
Host: login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded
Cookie: {cookie}
Content-Length: 941

client_id={Application (client) ID}
&scope=https://graph.microsoft.com/mail.read
&redirect_uri=http://localhost/myapp/
&grant_type=authorization_code
&client_secret={secret}
&code={code}

I used the Postman sample provided by Microsoft to find the root cause.

https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow#request-an-access-token

Siusiubhan answered 23/2, 2021 at 16:51 Comment(3)
Thanks men, it worked!!. Also note that your url and learn.microsoft.com/en-us/graph/auth-v2-user#token-request show us a encoded url in the example :SPiraeus
Thank you! This was the same problem I was having!Cybernetics
Oh man... thanks microsoft !msSnail
C
3

From the screenshot, it appears that the URLEncoding is incorrect. The '/' character in the path should be encoded to %2F, while your code has %2. (After '.com' and before 'Configurations'.)

Also, have you considered the Authorization Provider from the SDK? https://learn.microsoft.com/en-us/graph/sdks/choose-authentication-providers?tabs=CS#AuthCodeProvider

Cytology answered 4/9, 2019 at 14:0 Comment(1)
Ok, it was a good catch, I did fixed the value, see EDIT 1. Sadly, it did not fixed the problem.Emilio
I
2

You seem to be mixing the authorize and token endpoints.

If you want the user to authenticate, you have to redirect the user to that URL, not send a POST request to it. After the user returns to your app, you need to exchange the authorisation code for tokens. Documentation: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-web-app-call-api-overview

If you want a token just for your app without user authentication, you need to call the token endpoint. Documentation: https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-daemon-overview

Ibson answered 4/9, 2019 at 13:59 Comment(3)
I'm trying to use the login.microsoftonline.com{tenantId}/oauth2/v2.0/authorize endpoint to get an authorization code. This is why I'm doing a post resquest at login.microsoftonline.com{tenantId}/oauth2/v2.0/authorize. With the following parameters: client_id, response_type, redirect_uri, response_mode, client_secret, scope and state. As describe here: learn.microsoft.com/en-us/graph/auth-v2-user, the response should be a page where the user need to consent, then it should redirect to the redirect_uri after the consent.Emilio
You can't get an authorization code with a post request. You need to redirect a user browser there.Ibson
Redirect the user instead of doing a post to the url did fix the issue.Emilio
W
0

I also faced the same problem:

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

I tried few URL variants (with encoding, without, etc.) in Chrome, but was getting different exceptions about a wrong URL. Then I used the Safari browser and voila, I got a response code.

In the final result, I just copied the URL from the documentation, pasted tenant and client_id values from the registered application into the return_url parameter, and instead of the /myapp/ prefix I pasted %3A8080, where %3A it's the : symbol. The redirect_utl param has to be the same as URL in the registered application.

Waterresistant answered 15/8, 2022 at 14:8 Comment(0)
P
0

It worked for me without URL encoding. Maybe because the parameter is in the body and it's not a query parameter.

Postman example: postman example

Praemunire answered 7/2 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.