{"id":26973099,"url":"https://github.com/wargio/native-msal-cpp","last_synced_at":"2025-11-04T22:02:56.865Z","repository":{"id":212104601,"uuid":"730713851","full_name":"wargio/native-msal-cpp","owner":"wargio","description":"Microsoft MSAL C++ Example","archived":false,"fork":false,"pushed_at":"2025-02-11T08:09:21.000Z","size":22,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-03T09:41:37.749Z","etag":null,"topics":["cpp","msal"],"latest_commit_sha":null,"homepage":"https://kumo.みんな/2023-12-12.html","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wargio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-12T14:12:18.000Z","updated_at":"2025-02-12T02:37:35.000Z","dependencies_parsed_at":"2025-04-03T09:41:40.573Z","dependency_job_id":"865d8003-ec37-4d31-b470-439e59fb5189","html_url":"https://github.com/wargio/native-msal-cpp","commit_stats":null,"previous_names":["wargio/native-msal-cpp-windows64","wargio/native-msal-cpp"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/wargio/native-msal-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wargio%2Fnative-msal-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wargio%2Fnative-msal-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wargio%2Fnative-msal-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wargio%2Fnative-msal-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wargio","download_url":"https://codeload.github.com/wargio/native-msal-cpp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wargio%2Fnative-msal-cpp/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267317703,"owners_count":24068481,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","msal"],"created_at":"2025-04-03T09:41:21.757Z","updated_at":"2025-11-04T22:02:51.844Z","avatar_url":"https://github.com/wargio.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Native MSAL C++ Example using msalruntime\n\nThis is a C++ example which uses the Microsoft MSAL Runtime library available for the python MSAL module.\n\n## Usage\n\nIn this repository you will find 2 source folders\n\n- `sources` contains the examples using the library `msalruntime.dll`\n- `subproject` contains the meson `wrap` file that allows to download, unpack and include the library when building the examples.\n\n## How to build it\n\nInstall python and then, using `pip`, install `ninja` and `meson`, alternatively you can use `muon`.\n\nThen, open a developer command prompt and run the following commands\n\n```\nmeson setup --reconfigure --prefix=%CD%\\release -Dappid=\"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\" -Dauthority=\"yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy\" build\nninja -C build install\n```\n\nYou can then run any of the built examples from the `release/bin` folder.\n\n## FAQ\n\n- This code is pure native; there is no translation or calls to python and its modules.\n- You can embed this code to build C++ client applications which requires an AzureAD (Entra ID) token for the REST APIs.\n- All the calls made by the MSAL library are asyncronous.\n- You are required to initialize `msalruntime.dll` using the startup/shutdown functions.\n- You will need to ship your built executable with `msalruntime.dll`.\n\n## Example of interactive signin.\n\nFor more examples, please look at the folder `sources`.\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cMSALRuntime.h\u003e\n#include \u003cwindows.h\u003e\n\n// Your Microsoft Azure AD/Entra ID \"Application (client) ID\"\n#define APP_CLIENT_ID \"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\"\n\n// Your Microsoft Azure AD/Entra ID \"Directory (tenant) ID\"\n#define APP_AUTHORITY \\\n\t\"https://login.microsoftonline.com/yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy\"\n\nstatic void auth_callback(\n\t\tMSALRUNTIME_AUTH_RESULT_HANDLE authResult, void *callbackData) {\n\tbool *shouldWait = (bool *)callbackData;\n\n\tint32_t token_length = 0;\n\t// get token length.\n\tMSALRUNTIME_GetAccessToken(handle, nullptr, \u0026token_length);\n\tif (token_length \u003e 0) {\n\t\tos_char *token = new os_char[token_length];\n\t\tif (MSALRUNTIME_GetAccessToken(handle, token, \u0026token_length) ==\n\t\t\t\tMSALRUNTIME_SUCCEED) {\n\t\t\t// print the token\n\t\t\tstd::wcout \u003c\u003c token \u003c\u003c std::endl;\n\t\t}\n\t\tdelete[] token;\n\t}\n\n\t// free memory\n\tMSALRUNTIME_ReleaseAuthResult(authResult);\n\n\t// let's stop waiting for this async callback\n\t*shouldWait = false;\n}\n\nstatic HWND consoleWindow() {\n\treturn GetAncestor(GetConsoleWindow(), 3);\n}\n\nint main(int argc, char const *argv[]) {\n\t// Some vars to wait for reply\n\tbool shouldWait = true;\n\n\t// MSAL required variables\n\tMSALRUNTIME_AUTH_PARAMETERS_HANDLE authParameters = 0;\n\tMSALRUNTIME_ASYNC_HANDLE asyncHandle = 0;\n\n\t// get parent window handler\n\tauto parentHwnd = consoleWindow();\n\n\t// create a random correlation uuid (can be empty)\n\tstd::wstring correlationId = L\"\";\n\n\t// initialize msalruntime\n\tMSALRUNTIME_Startup();\n\n\t// set pii\n\tMSALRUNTIME_SetIsPiiEnabled(0);\n\n\t// create auth parameters and configure the app.\n\tMSALRUNTIME_CreateAuthParameters(\n\t\t\tAPP_CLIENT_ID, APP_AUTHORITY, \u0026authParameters);\n\n\t// sets the requested scopes (mandatory and cannot be an empty string).\n\tMSALRUNTIME_SetRequestedScopes(\n\t\t\tauthParameters, L\"https://graph.microsoft.com/.default\");\n\n\t// configure the redirect uri to use the secure broker popup\n\t// msal for python sets this as 'placeholder'.\n\tMSALRUNTIME_SetRedirectUri(authParameters, L\"placeholder\");\n\n\t// set the secure broker popup to not block this thread.\n\tMSALRUNTIME_SetAdditionalParameter(\n\t\t\tauthParameters, L\"msal_gui_thread\", L\"true\");\n\n\t// finally try to sign in.\n\t// You can get the accountHint (i.e. username via MSALRUNTIME_DiscoverAccountsAsync)\n\tMSALRUNTIME_SignInAsync(parentHwnd, authParameters, correlationId.c_str(),\n\t\t\tL\"\", auth_callback, \u0026shouldWait, \u0026asyncHandle);\n\n\t// this is a terrible way to wait for async, but this is\n\t// an example, so please use locks \u0026 signals.\n\twhile (shouldWait) {\n\t\tSleep(300);\n\t}\n\n\t// free the async handle\n\tMSALRUNTIME_ReleaseAsyncHandle(asyncHandle);\n\n\t// free auth parameters\n\tMSALRUNTIME_ReleaseAuthParameters(authParameters);\n\n\t// deinitialize msalruntime\n\tMSALRUNTIME_Shutdown();\n\treturn 0;\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwargio%2Fnative-msal-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwargio%2Fnative-msal-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwargio%2Fnative-msal-cpp/lists"}