Skip to content

Instantly share code, notes, and snippets.

@cheenamalhotra
Created September 24, 2020 16:28
Show Gist options
  • Save cheenamalhotra/8398e61f8c50388382c6dd0e84609720 to your computer and use it in GitHub Desktop.
Save cheenamalhotra/8398e61f8c50388382c6dd0e84609720 to your computer and use it in GitHub Desktop.
Test Token Acquire from MSAL library
using System;
using Microsoft.Identity.Client;
namespace TestMSAL
{
class Program
{
static void Main()
{
RunServicePincipalTest();
}
private static void RunServicePincipalTest()
{
// Format: https://login.windows.net/<tenant-id>/
string authority = "";
string appId = "";
string appSecret = "";
string scope = "https://database.windows.net//.default";
string[] scopes = new string[] { scope };
IConfidentialClientApplication ccApp = ConfidentialClientApplicationBuilder.Create(appId)
.WithAuthority(authority)
.WithClientSecret(appSecret)
.WithClientName("Test Application")
.WithClientVersion("1.0")
.Build();
AuthenticationResult result = ccApp.AcquireTokenForClient(scopes).ExecuteAsync().Result;
Console.WriteLine("Access Token " + result.AccessToken);
Console.WriteLine("Expires On " + result.ExpiresOn);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.18.0" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment