Azure-Samples / Azure-Samples/ms-identity-ciam-dotnet-tutorial

Null Exception when User X's out of the login process

Open
#144 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
48
Forks
35
PR merge metrics
No merged PRs in 30d

Description

MSALClientHelper.cs line 164 or there abouts.

Last line of the methhid SignInUserAndAcquireAccessToken this.AuthResult.AccessToken; throws a null exception because this.AuthResult is null when the user X's out of the login process. Is this by design and we should wrap the call with a try catch? Or should this be caught in the method and handled... or throw a new exception "User Closed Login Process"?

Thanks
Brady

```
public async Task SignInUserAndAcquireAccessToken(string[] scopes)
{
Exception.ThrowOn(() => this.PublicClientApplication == null, PCANotInitializedExceptionMessage);

var existingUser = await FetchSignedInUserFromCache().ConfigureAwait(false);

try
{
// 1. Try to sign-in the previously signed-in account
if (existingUser != null)
{
this.AuthResult = await this.PublicClientApplication
.AcquireTokenSilent(scopes, existingUser)
.ExecuteAsync()
.ConfigureAwait(false);
}
else
{
this.AuthResult = await SignInUserInteractivelyAsync(scopes);
}
}
catch (MsalUiRequiredException ex)
{
// A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenInteractive to acquire a token interactively
Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

this.AuthResult = await this.PublicClientApplication
.AcquireTokenInteractive(scopes)
.ExecuteAsync()
.ConfigureAwait(false);
}
catch (MsalException msalEx)
{
Debug.WriteLine($"Error Acquiring Token interactively:{Environment.NewLine}{msalEx}");
}

return this.AuthResult.AccessToken;
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.