MSAL.Net code to call Easy Auth Function App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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