Skip to content

Instantly share code, notes, and snippets.

@bachoang
Created August 11, 2019 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bachoang/9f03df153b77e7cf78041f3fba4880f5 to your computer and use it in GitHub Desktop.
Save bachoang/9f03df153b77e7cf78041f3fba4880f5 to your computer and use it in GitHub Desktop.
MSAL.Net code to call Easy Auth Function App
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using System.Net.Http;
namespace MSALConsoleApp
{
class Program
{
static string[] scopes = new[] { "https://easyauthfunctest.azurewebsites.net/user_impersonation" };
static string ClientId = "<Application ID>";
static string Tenant = "<Directory ID>";
public static IPublicClientApplication app;
static void Main(string[] args)
{
AuthenticationResult result = GetToken().Result;
if (result != null)
{
string url = "https://easyauthfunctest.azurewebsites.net/api/HttpTrigger1?code=zIrQXHC9ypU2ewa4YadZfXgka7XgNG/U7J/kDGW79aXig3q907jo2A==&name=Azure";
var content = SendRequest(url, result.AccessToken).Result;
Console.WriteLine($"response from Azure Function App: {content}\r\n");
}
Console.ReadKey();
// Go to http://aka.ms/dotnet-get-started-console to continue learning how to build a console app!
}
public static async Task<AuthenticationResult> GetToken()
{
app = PublicClientApplicationBuilder.Create(ClientId)
.WithTenantId(Tenant)
.WithRedirectUri("myapp://auth")
.Build();
var accounts = await app.GetAccountsAsync();
var firstAccount = accounts.FirstOrDefault();
AuthenticationResult authResult = null;
try
{
authResult = await app.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
}
catch (MsalUiRequiredException ex)
{
Console.WriteLine($"MsalUiRequiredException: {ex.Message}\r\n");
try
{
authResult = await app.AcquireTokenInteractive(scopes)
.WithAccount(accounts.FirstOrDefault())
.WithPrompt(Prompt.SelectAccount)
.ExecuteAsync();
}
catch (MsalException msalex)
{
Console.WriteLine($"Error Acquiring Token:{System.Environment.NewLine}{msalex}\r\n");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}\r\n");
}
if (authResult != null)
{
Console.WriteLine($"access token: {authResult.AccessToken}\r\n");
Console.WriteLine($"id token: {authResult.IdToken}\r\n");
}
return authResult;
}
public static async Task<string> SendRequest(string url, string token)
{
var httpClient = new HttpClient();
HttpResponseMessage response;
try
{
var request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
return content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment