how to use Microsoft identity platform and OAuth 2.0 authorization code flow from Desktop app

Manoj Pant 135 Reputation points
2023-04-26T07:00:07.3066667+00:00

Currently, we are using OAuth2.0 authorization code flow into MVC web application, and its working fine as per our requirements, now same things we also need to implement it into our VB.net desktop application: Below are steps use into MVC web application:

  1. App Register into Azure : Register the app and set a redirect url to collect the authorized code: Web:- https://localhost:44337/Oauth/callback
  2. Code to get Authorized code, access token, and refresh token:
 var redirectUrl = "https://login.microsoftonline.com/3c150d40-f8a6-4418-b062-26ad5b6608dd/oauth2/v2.0/authorize?" +
                "client_id=fe97ed3f-091b-4b86-88d8-dcce141571e1" +
                "&response_type=code" +
                "&redirect_uri=https://localhost:44337/Oauth/callback" +  // need to put correct value in it. (redirect URL during app registration)
                "&response_mode=query" +
                "&scope= User.Read offline_access" +
                "&state=12345";              
           
            return Redirect(redirectUrl);
  1. Collect auth code into redirect url:
 public ActionResult callback(string code, string state, string error) 
        {

            if (!string.IsNullOrWhiteSpace(code))
            {

                RestClient restClient = new RestClient("https://login.microsoftonline.com/3c150d40-f8a6-4418-b062-26ad5b6608dd/oauth2/v2.0/token?");
                RestRequest restRequest = new RestRequest();
            

                restRequest.AddParameter("client_id", "fe97ed3f-091b-4b86-88d8-dcce141571e1");
                restRequest.AddParameter("scope", "User.Read offline_access");
                restRequest.AddParameter("grant_type", "authorization_code");
                restRequest.AddParameter("code", code);
                restRequest.AddParameter("redirect_uri", "https://localhost:44337/Oauth/callback");
                restRequest.AddParameter("client_secret", "E4K8Q~60nN~Bgpm31a5d3UeOGzfet93THIembcZP");

               

                var response = restClient.Post(restRequest);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var customerDto = JsonConvert.DeserializeObject<Token>(response.Content);
                    

                }


                
            }
           
        }
Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
3,902 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,752 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,726 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Shweta Mathur 28,021 Reputation points Microsoft Employee
    2023-04-27T07:20:49.6466667+00:00

    Hi @Manoj Pant ,

    Thanks for reaching out.

    Microsoft Identity Platform provide reference samples for desktop applications which are using MSAL libraries to acquire token directly using Authorization Code flow :

    https://learn.microsoft.com/en-us/azure/active-directory/develop/sample-v2-code#desktop

    Hope this will help.

    Thanks,

    Shweta

    Please remember to "Accept Answer" if answer helped you.

    0 comments No comments