Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

URL encode client secret #10

Closed
pdx-mikaelberg opened this issue Jul 15, 2021 · 2 comments
Closed

URL encode client secret #10

pdx-mikaelberg opened this issue Jul 15, 2021 · 2 comments

Comments

@pdx-mikaelberg
Copy link


It'd be a good idea to URL encode the client secret here, since there is no limitation on what characters it may contain. I used one containing the % sign and ended up getting 401s.

URL encoding the secret solved it:

    var encodedSecret = System.Web.HttpUtility.UrlEncode(_clientSecret);
    var requestBody = $"grant_type=client_credentials&client_id={_clientId}" +
                                  $"&client_secret={encodedSecret}" +
                                  $"&resource={audience}";
@CameronGoodwin
Copy link
Contributor

@pdx-mikaelberg
Thank you for this feedback! I am working on some updates right now so I will work on doing this. Sorry for the long delayed response as I was occupied with other engagements and just now circling back to make some of the feedback updates.

@CameronGoodwin
Copy link
Contributor

CameronGoodwin commented Sep 3, 2021

Here is what I added / updated. I thought about encoding the whole body to be safe, but decided to just go with the client secret as the other values should not have the characters that would cause issues. Then it makes the calls easier to read in Fiddler or other debugging tools.

            //  URL encode the Secret key to ensure it gets properly transmitted if containing
            //  characters such as '%'.  We just encode the secret so the rest of the body is
            //  easily read in debugging tools such as Fiddler.
            var encodedSecret = System.Web.HttpUtility.UrlEncode(_clientSecret);

            //  Build the HTTP request information to generate the access token
            var requestUri = $"https://login.microsoftonline.com/{_tenantId}/oauth2/token";
            var httpRequest = new HttpRequestMessage(HttpMethod.Post, requestUri.ToString());
            var requestBody = $"grant_type=client_credentials&client_id={_clientId}" +
                              $"&client_secret={encodedSecret}" +
                              $"&resource={audience}";
            httpRequest.Content = new StringContent(requestBody, Encoding.UTF8, "application/x-www-form-urlencoded");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants