Answered by:
Getting Authorization code in c#

Question
-
Hi All,
I need to get auth code for getting access token from https://login.microsoftonline.com.
But my code is not working. Please advise what i am doing wrong. Call to api works perfectly from Postman.
HttpClient client = new HttpClient();
var builder = new UriBuilder("https://login.microsoftonline.com/xxxx/oauth2/authorize");
builder.Port = -1;
var query = HttpUtility.ParseQueryString(builder.Query);
query["resource"] = "https://xxxx";
query["response_type"] = "code";
query["response_mode"] = "query";
query["state"] = "1234";
query["scope"] = "openid";
query["client_id"] = "xxx";
query["client_secret"] = "xxx";
query["callback_url"] = "https://www.getpostman.com/oauth2/callback";
builder.Query = query.ToString();
string url = builder.ToString();
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var response = await client.GetAsync(url);
var res = await response.Content.ReadAsStringAsync();
return res;
Pooja Jagtap Software Engineer KPIT Cummins
Friday, March 13, 2020 6:09 PM
Answers
-
Yes that is correct any workflow which runs as a service or triggered as long as long as not by a human needs to implement client credentials workflow. Authorization code grant works when you have a user to authorize.
Mandar Dharmadhikari
- Marked as answer by Pooja Jagtap Thursday, April 2, 2020 1:56 PM
Thursday, April 2, 2020 10:06 AMModerator
All replies
-
Have you tried debugging and stepping through the code?
Are you getting any error messages?
Under what context is this code being executed?
Sunday, March 15, 2020 11:51 PM -
Hi Colin,
we are trying to call FHIR API in Biztalk, for which I have to get Access Token, but before that I need to get Auth code. I was trying to write simple C# class library.
Yes, I am getting error but it is not clear error message. It is long HTML. It might be failing due to multifact authentication, as I see the error message as, we cant sign you in, please enter valid email id or mobile number.
Pooja Jagtap Software Engineer KPIT Cummins
- Edited by Pooja Jagtap Monday, March 16, 2020 4:35 PM typo
Monday, March 16, 2020 12:26 PM -
That error looks fairly clear to me, you aren't passing it a client_id it recognizes.Monday, March 16, 2020 7:49 PM
-
The problem is, URL is redirecting to login page where I have to enter user name and pwd. Next I will get code to connect on mobile after giving that getting auth code in response.
Is there any way to implement this in Biztalk. Please advise.
Thanks.
Pooja Jagtap Software Engineer KPIT Cummins
Thursday, March 26, 2020 11:54 AM -
Hi Pooja,
The work flow you are talking about is called the Authorization Code Grant workflow in OAUTH 2.0 terms. This workflow is used when the user CAN authorize the website to query data on their behalf. Hence the name Authorization Code Grant. The main requirement for this flow is "User needs to manually authorize" the request.
In case of BizTalk that is not the case. BizTalk is a middleware platform and it is in turn triggered by external services. So BizTalk is basically a daemon or a service. In such cases it is NOT possible to implement Authorization Code Grant workflow of OAUTH 2.0 . In such cases you need to implement Client Credentials Grant workflow.
So the flow you are asking is not possible in BizTalk.
Mandar Dharmadhikari
- Proposed as answer by Bobby Biztalk Wednesday, April 1, 2020 8:30 PM
Wednesday, April 1, 2020 2:59 AMModerator -
Hi Mandar, Thanks for your reply. So is it not possible to implement grant type Auth Code in logic apps as well. yes, Client credentials works fine.
Pooja Jagtap Software Engineer KPIT Cummins
Thursday, April 2, 2020 9:39 AM -
Yes that is correct any workflow which runs as a service or triggered as long as long as not by a human needs to implement client credentials workflow. Authorization code grant works when you have a user to authorize.
Mandar Dharmadhikari
- Marked as answer by Pooja Jagtap Thursday, April 2, 2020 1:56 PM
Thursday, April 2, 2020 10:06 AMModerator