Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

MFC Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(MFC) Get an Azure AD Access Token

Demonstrates how to obtain an Azure AD access token for authentication using a client ID, client secret, and tenant ID.

Chilkat C/C++ Library Downloads

MS Visual C/C++ Libs

See Also: Using MFC CString in Chilkat

#include <CkSocket.h>
#include <CkAuthAzureAD.h>
#include <CkJsonObject.h>
#include <CkFileAccess.h>
#include <CkStringArray.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    CkString strOut;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkSocket socket;

    // Make a TLS connection to login.microsoftonline.com, waiting at most 5000 milliseconds.
    bool success = socket.Connect("login.microsoftonline.com",443,true,5000);
    if (success != true) {
        strOut.append(socket.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Create an Azure AD auth object, and provide the required information for authorization.
    CkAuthAzureAD azureAD;
    azureAD.put_ClientId("AZURE_AD_CLIENT_ID");
    azureAD.put_ClientSecret("AZURE_AD_CLIENT_SECRET");
    azureAD.put_TenantId("AZURE_TENANT_ID");

    azureAD.put_Resource("https://outlook.office365.com/");

    // Retrieve the access token using the TLS connection to login.microsoftonline.com
    success = azureAD.ObtainAccessToken(socket);
    if (success != true) {
        strOut.append(socket.lastErrorText());
        strOut.append("\r\n");
        SetDlgItemText(IDC_EDIT1,strOut.getUnicode());
        return;
    }

    // Show the access token, and then save it to a JSON file
    // for future use (such as with a REST method call).
    strOut.append("Azure AD Access Token = ");
    strOut.append(azureAD.accessToken());
    strOut.append("\r\n");

    CkJsonObject json;
    json.AppendString("accessToken",azureAD.accessToken());

    // Save our access token to a file.  It will be used in subsequent REST API calls.
    CkFileAccess fac;
    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)
    CkStringArray sa;
    sa.SplitAndAppend(json.stringOf("accessToken"),".");

    CkStringBuilder sbIdToken;
    // The 2nd string is at index 1.
    sbIdToken.Append(sa.getString(1));
    sbIdToken.Decode("base64","utf-8");

    CkJsonObject jsonIdToken;
    jsonIdToken.LoadSb(sbIdToken);
    jsonIdToken.put_EmitCompact(false);
    strOut.append(jsonIdToken.emit());
    strOut.append("\r\n");

    // 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"
    // }

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    // Get the desired information from the JSON:
    const char *aud = jsonIdToken.stringOf("aud");
    const char *iss = jsonIdToken.stringOf("iss");
    int iat = jsonIdToken.IntOf("iat");
    int nbf = jsonIdToken.IntOf("nbf");
    int exp = jsonIdToken.IntOf("exp");
    const char *aio = jsonIdToken.stringOf("aio");
    const char *app_displayname = jsonIdToken.stringOf("app_displayname");
    const char *appid = jsonIdToken.stringOf("appid");
    const char *appidacr = jsonIdToken.stringOf("appidacr");
    const char *idp = jsonIdToken.stringOf("idp");
    const char *oid = jsonIdToken.stringOf("oid");
    const char *sid = jsonIdToken.stringOf("sid");
    const char *sub = jsonIdToken.stringOf("sub");
    const char *tid = jsonIdToken.stringOf("tid");
    const char *uti = jsonIdToken.stringOf("uti");
    const char *ver = jsonIdToken.stringOf("ver");


    SetDlgItemText(IDC_EDIT1,strOut.getUnicode());

    }

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.