Beispiel #1
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        /// <summary>
        /// Force a sign out of the current user. NOTE: Currently MSAL only can remove a user from the Cache.
        /// </summary>
        /// <returns>The out.</returns>
        public async Task SignOut()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            foreach (IUser user in PCA.Users)
            {
                PCA.Remove(user);
            }

            Name   = null;
            UserId = null;

            authResultForScopes.Clear();

            AuthenticationChanged?.Invoke(null, null);
        }
Beispiel #2
0
 protected virtual void OnAuthenticationChanged(ConnectEventArgs e)
 {
     //ConnectEventArgs.AuthenticationTypes authType = ConnectEventArgs.AuthenticationTypes.Windows;
     ////Windows Authentication; SQL Server Authentication
     //switch (cboAuthentication.Text)
     //{
     //    case "Windows Authentication":
     //        authType = ConnectEventArgs.AuthenticationTypes.Windows;
     //        break;
     //    case "SQL Server Authentication":
     //        authType = ConnectEventArgs.AuthenticationTypes.SQL;
     //        break;
     //}
     //ConnectEventArgs args = new ConnectEventArgs(authType, txtUserName.Text, txtPassword.Text, txtDatabase.Text, txtServer.Text);
     AuthenticationChanged?.Invoke(this, e);
 }
Beispiel #3
0
 private void RaiseAuthenticationChanged()
 {
     AuthenticationChanged?.Invoke(this, EventArgs.Empty);
 }
Beispiel #4
0
 private void RestClientOnAuthenticationChanged(object sender, EventArgs e)
 {
     AuthenticationChanged?.Invoke(this, EventArgs.Empty);
 }
Beispiel #5
0
        /// <summary>
        /// Gets an access token for the current user.  Optionally if no token is available, we can prompt the user to sign in.
        /// </summary>
        /// <returns>The token.</returns>
        /// <param name="signInIfSilentFails">If set to <c>true</c> sign in if silent fails.</param>
        public async Task <string> GetToken(string[] Scopes, bool signInIfSilentFails = false)
        {
            try
            {
                //// Hack for MSAL Tokens not storing on the simulator
                //// On a real device, we want to check the cache every time for the access token.
                if (authResultForScopes.ContainsKey(Scopes) && _simCheck.CheckIfSimulator())
                {
                    return(authResultForScopes[Scopes].AccessToken);
                }

                // Attempt to perform silent authentication (i.e. use previous authentication/refresh token.
                var authResult = await PCA.AcquireTokenSilentAsync(Scopes, PCA.Users.FirstOrDefault());

                authResultForScopes.Remove(Scopes);
                authResultForScopes.Add(Scopes, authResult);

                Name   = authResult?.User?.Name;
                UserId = authResult?.User?.DisplayableId;

                await _logger.LogMessage($"Adding Scope(s): {String.Join(", ", Scopes)} to cache.", "Authentication");

                AuthenticationChanged?.Invoke(authResult.AccessToken, Scopes);

                return(authResult.AccessToken);
            }
            catch (Exception ex)
            {
                await _logger.LogException(ex).ConfigureAwait(false);

                await _logger.LogMessage("Unable to get access token silently.", "Authentication").ConfigureAwait(false);

                if (signInIfSilentFails)
                {
                    try
                    {
                        // Attempt to perform an interactive login.
                        var authResult = await PCA.AcquireTokenAsync(Scopes, UiParent);

                        authResultForScopes.Remove(Scopes);
                        authResultForScopes.Add(Scopes, authResult);

                        Name   = authResult?.User?.Name;
                        UserId = authResult?.User?.DisplayableId;

                        await _logger.LogMessage($"Adding Scope(s): {String.Join(", ", Scopes)} to cache.", "Authentication");

                        AuthenticationChanged?.Invoke(authResult.AccessToken, Scopes);

                        return(authResult.AccessToken);
                    }
                    catch (Exception ex2)
                    {
                        await _logger.LogException(ex2).ConfigureAwait(false);

                        await _logger.LogMessage("Unable to perform authentication.", "Authentication").ConfigureAwait(false);
                    }
                }
            }

            // Return null if we weren't able to authenticate the user.
            return(null);
        }
Beispiel #6
0
 public void RaiseAuthenticationChanged(string userAlias, bool authenticated)
 {
     AuthenticationChanged?.Invoke(this, new AuthenticationEventArgs(userAlias, authenticated, true));
 }