Created
May 30, 2020 16:30
-
-
Save manoj-choudhari-git/125957c0e8fd3744908accb06e6aad69 to your computer and use it in GitHub Desktop.
Azure AD B2C with Wpf App and Web API
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
/// <summary> | |
/// Acquires token and then gives call to API | |
/// </summary> | |
/// <param name="sender">The sender</param> | |
/// <param name="e">The event arguments</param> | |
private async void CallApiButton_Click(object sender, RoutedEventArgs e) | |
{ | |
//// Try to Acquire token silently | |
//// If not possible, then try to acquire it interactively. | |
AuthenticationResult authResult = null; | |
var app = App.PublicClientApp; | |
IEnumerable<IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); | |
try | |
{ | |
authResult = await app.AcquireTokenSilent(App.ApiScopes, GetAccountByPolicy(accounts, App.PolicySignUpSignIn)) | |
.ExecuteAsync(); | |
} | |
catch (MsalUiRequiredException ex) | |
{ | |
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. | |
// This indicates you need to call AcquireTokenAsync to acquire a token | |
Debug.WriteLine($"MsalUiRequiredException: {ex.Message}"); | |
try | |
{ | |
authResult = await app.AcquireTokenInteractive(App.Scopes) | |
.ExecuteAsync(); | |
} | |
catch (MsalException msalex) | |
{ | |
ResultText.Text = $"Error Acquiring Token:{Environment.NewLine}{msalex}"; | |
} | |
} | |
catch (Exception ex) | |
{ | |
ResultText.Text = $"Error Acquiring Token Silently:{Environment.NewLine}{ex}"; | |
return; | |
} | |
//// After token is available | |
//// Call the API and Show the result | |
if (authResult != null) | |
{ | |
if (string.IsNullOrEmpty(authResult.AccessToken)) | |
{ | |
ResultText.Text = "Access token is null (could be expired). Please do interactive log-in again."; | |
} | |
else | |
{ | |
ResultText.Text = await GetHttpContentWithToken(App.ApiEndpoint, authResult.AccessToken); | |
DisplayUserInfo(authResult); | |
} | |
} | |
} | |
/// <summary> | |
/// Perform an HTTP GET request to a URL using an HTTP Authorization header | |
/// </summary> | |
/// <param name="url">The URL</param> | |
/// <param name="token">The token</param> | |
/// <returns>String containing the results of the GET operation</returns> | |
public async Task<string> GetHttpContentWithToken(string url, string token) | |
{ | |
var httpClient = new HttpClient(); | |
HttpResponseMessage response; | |
try | |
{ | |
var request = new HttpRequestMessage(HttpMethod.Get, url); | |
request.Headers.Authorization = new 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