0% found this document useful (0 votes)
7 views1 page

Gettokenscript

The document contains C# code for a console application that uses the Microsoft Identity Client library to acquire and manage a refresh token for Azure authentication. It sets up a public client application, retrieves an interactive token, and copies the refresh token to the clipboard. The application also changes the console text color to indicate successful token retrieval.

Uploaded by

aki11cricketer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Gettokenscript

The document contains C# code for a console application that uses the Microsoft Identity Client library to acquire and manage a refresh token for Azure authentication. It sets up a public client application, retrieves an interactive token, and copies the refresh token to the clipboard. The application also changes the console text color to indicate successful token retrieval.

Uploaded by

aki11cricketer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

$code = @"

using Microsoft.Identity.Client;
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace MsalExtensions
{
public class Program
{
[STAThread]
public static void Main()
{
var app = PublicClientApplicationBuilder.Create("41a86758-55ef-4635-
963a-2e60330dd930")
.WithAuthority(AzureCloudInstance.AzurePublic, "72f988bf-86f1-41af-
91ab-2d7cd011db47")
.WithRedirectUri("http://localhost")
.Build();

string refreshToken = null;


app.UserTokenCache.SetAfterAccess((TokenCacheNotificationArgs args) =>
{
var cacheBytes = args.TokenCache.SerializeMsalV3();
refreshToken = Regex.Match(Encoding.UTF8.GetString(cacheBytes),
@"(?is)""RefreshToken"".*?""secret""\:\s*""(.*?)""").Groups[1].Value;
});

var result = app.AcquireTokenInteractive(new[] {


"https://management.core.windows.net//user_impersonation" }).ExecuteAsync().Result;
Clipboard.SetText(refreshToken);

var initialColor = Console.ForegroundColor;


Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("\r\n\r\n\r\n ---=== The REFRESH token has been
copied to clipboard ===--- \r\n\r\n\r\n");
Console.ForegroundColor = initialColor;
}
}
}
"@

$assembliesPath = '.\NugetPackages\Microsoft.Identity.Client.4.18.0\lib\net461\
Microsoft.Identity.Client.dll'
$assemblies = ($assembliesPath, 'System.Windows.Forms')
Add-Type -Path $assembliesPath
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp

iex "[MsalExtensions.Program]::Main()"

You might also like