/// <summary> /// Force to signin the user in an interactive authentication flow. /// </summary> /// <returns></returns> public async Task ForceInteractiveSignIn() { try { OnAuthenticationChanged?.Invoke(this, AuthenticationState.StartedInteractive); var authResult = await identityClientApp.AcquireTokenInteractive(grantScopes).ExecuteAsync(); // Set access token and expiration TokenForUser = authResult.AccessToken; Expiration = authResult.ExpiresOn; OnAuthenticationChanged?.Invoke(this, AuthenticationState.Completed); } catch (Exception e) when(e is PlatformNotSupportedException || e is MsalUiRequiredException) { var authResult = await identityClientApp.AcquireTokenWithDeviceCode(grantScopes, dcr => { OnAuthenticationChanged?.Invoke(this, AuthenticationState.FallbackToDeviceCode); OnPresentDeviceCode?.Invoke(this, dcr); return(Task.FromResult(0)); }).ExecuteAsync(); // Set access token and expiration TokenForUser = authResult.AccessToken; Expiration = authResult.ExpiresOn; OnAuthenticationChanged?.Invoke(this, AuthenticationState.Completed); } catch (Exception) { OnAuthenticationChanged?.Invoke(this, AuthenticationState.Failed); } }
public async Task Logout() { isAuthenticated = false; OnAuthenticationChanged?.Invoke(this, EventArgs.Empty); //await authServiceClient.Logout(); if (cachingService.Exists("user")) { cachingService.Remove("user"); } }
/// <summary> /// Signs the user out of the service. /// </summary> public async Task SignOutAsync() { foreach (IAccount account in await identityClientApp.GetAccountsAsync()) { await identityClientApp.RemoveAsync(account); } GraphClient = GetAuthenticatedClient(); // Reset token and expiration Expiration = DateTimeOffset.MinValue; TokenForUser = null; OnAuthenticationChanged?.Invoke(this, AuthenticationState.SignOut); }
public async Task <User> AuthenticateAsync(string username, string password) { try { if (AreCredentialsAvaible() && HasValidCredentialsAvaible()) { return(GetStoredUser()); } var encryptedPassword = crypto.Encrypt(password); var userModel = await authServiceClient.Login(new UserModel { Email = username, Password = encryptedPassword }); var user = new User { Id = userModel.Id, Email = userModel.Email, Password = password, ChangeStamp = DateTime.Now, LastApiCall = DateTime.Now, Token = userModel.Token }; await SaveOrUpdateUser(user); return(user); } catch (Exception ex) { throw ex; } finally { isAuthenticated = true; OnAuthenticationChanged?.Invoke(this, EventArgs.Empty); } }