How can I retrieve Oauth2 authorization code

Viewed 80

I am programatically trying to retrieve the authorization code that is returned when this URL is posted.

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=<client_id>&response_type=code&redirect_uri=https://xyzzy.portal.azure-api.net/docs/services/oauth-server-1/console/oauth2/authorizationcode/callback&response_mode=query&scope=api://qwerty/API.Read&state=12345

When I test this in postman, I have two steps. One that generates that URL, which I then take an post into a browser and I get a response_type=somelongcodehere back in the URL address. In C# I thought I could do something as simple as

            var c2 = new RestClient(ResponseUri); //where ResponseUri is the URL address
            c2.Timeout = -1;
            var r2 = new RestRequest(Method.POST);
            IRestResponse v2 = c2.Execute(r2);

and that somewhere in the response from that I should be able to see the 'code' and then be able to continue.

I know it shouldn't be this difficult and that I'm missing something painfully obvious. Any help would be appreciated.

Thanks,

1 Answers

You need to use HttpListener class and wait for the response. Then you get full url and parse it.

Related