Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(C# UWP/WinRT) Get an Azure AD Access TokenDemonstrates how to obtain an Azure AD access token for authentication using a client ID, client secret, and tenant ID.
// This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Chilkat.Socket socket = new Chilkat.Socket(); // Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds. bool success = await socket.ConnectAsync("login.microsoftonline.com",443,true,5000); if (success != true) { Debug.WriteLine(socket.LastErrorText); return; } // Create an Azure AD auth object, and provide the required information for authorization. Chilkat.AuthAzureAD azureAD = new Chilkat.AuthAzureAD(); azureAD.ClientId = "AZURE_AD_CLIENT_ID"; azureAD.ClientSecret = "AZURE_AD_CLIENT_SECRET"; azureAD.TenantId = "AZURE_TENANT_ID"; azureAD.Resource = "https://outlook.office365.com/"; // Retrieve the access token using the TLS connection to login.microsoftonline.com success = await azureAD.ObtainAccessTokenAsync(socket); if (success != true) { Debug.WriteLine(socket.LastErrorText); return; } // Show the access token, and then save it to a JSON file // for future use (such as with a REST method call). Debug.WriteLine("Azure AD Access Token = " + azureAD.AccessToken); Chilkat.JsonObject json = new Chilkat.JsonObject(); json.AppendString("accessToken",azureAD.AccessToken); // Save our access token to a file. It will be used in subsequent REST API calls. Chilkat.FileAccess fac = new Chilkat.FileAccess(); fac.WriteEntireTextFile("qa_data/tokens/azureAD_office365.json",json.Emit(),"utf-8",false); // Let's get the id information out of the access token. // Our JSON looks like this: // {"accessToken":"eyJ0eXAiO---TdjT3RjIn0.eyJhdWQiOiJo---jEuMCJ9.CIx0sUT8s---KvzKKUw"} // I used "---" instead of "..." to indicate a large quantity of omitted chars. // The accessToken is a long string composed of 3 base64 strings concatenated with "." chars. // The 1st part is the JOSE header. If you decode from base64, you'll get the JSON JOSE header. // The 2nd part is the id token. When decoded this is the JSON that contains information about the authenticated application. // The 3rd part is binary and does not decode to JSON. // Let's get the information from the 2nd part (the id token) Chilkat.StringArray sa = new Chilkat.StringArray(); sa.SplitAndAppend(json.StringOf("accessToken"),"."); Chilkat.StringBuilder sbIdToken = new Chilkat.StringBuilder(); // The 2nd string is at index 1. sbIdToken.Append(sa.GetString(1)); sbIdToken.Decode("base64","utf-8"); Chilkat.JsonObject jsonIdToken = new Chilkat.JsonObject(); jsonIdToken.LoadSb(sbIdToken); jsonIdToken.EmitCompact = false; Debug.WriteLine(jsonIdToken.Emit()); // We have something like this: // { // "aud": "https://outlook.office365.com/", // "iss": "https://sts.windows.net/6e8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/", // "iat": 1586350465, // "nbf": 1586350465, // "exp": 1586354365, // "aio": "42dgYNjyOtX8ZZB7JLfMFJGeKUmvAA==", // "app_displayname": "ChilkatTest", // "appid": "f125d695-c50e-456e-a578-a486f06d1213", // "appidacr": "1", // "idp": "https://sts.windows.net/6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd/", // "oid": "7545a2fd-3f0d-48a4-9c58-a1a5700a24b1", // "sid": "ab981252-9378-4d0c-964b-eb2e1451138f", // "sub": "7546a2fc-3f0d-48a4-9c58-a1a5700a24b1", // "tid": "6d8ddd66-68d1-43b0-af5c-e31b4b7dd5cd", // "uti": "huIJBAa1tvGpczwV5S1BAA", // "ver": "1.0" // } // Get the desired information from the JSON: string aud = jsonIdToken.StringOf("aud"); string iss = jsonIdToken.StringOf("iss"); int iat = jsonIdToken.IntOf("iat"); int nbf = jsonIdToken.IntOf("nbf"); int exp = jsonIdToken.IntOf("exp"); string aio = jsonIdToken.StringOf("aio"); string app_displayname = jsonIdToken.StringOf("app_displayname"); string appid = jsonIdToken.StringOf("appid"); string appidacr = jsonIdToken.StringOf("appidacr"); string idp = jsonIdToken.StringOf("idp"); string oid = jsonIdToken.StringOf("oid"); string sid = jsonIdToken.StringOf("sid"); string sub = jsonIdToken.StringOf("sub"); string tid = jsonIdToken.StringOf("tid"); string uti = jsonIdToken.StringOf("uti"); string ver = jsonIdToken.StringOf("ver"); |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.