private async Task AuthenticateSucceed(AbpAuthenticateResultModel result) { AuthenticateResultModel = result; if (AuthenticateResultModel.ShouldResetPassword) { await UserDialogs.Instance.AlertAsync(L.Localize("ChangePasswordToLogin"), L.Localize("LoginFailed"), L.Localize("Ok")); return; } if (AuthenticateResultModel.RequiresTwoFactorVerification) { await _navigationService.SetMainPage <SendTwoFactorCodeView>(AuthenticateResultModel); return; } if (!AbpAuthenticateModel.IsTwoFactorVerification) { await _dataStorageService.StoreAuthenticateResultAsync(AuthenticateResultModel); } AbpAuthenticateModel.Password = null; await SetCurrentUserInfoAsync(); await UserConfigurationManager.GetAsync(); await _navigationService.SetMainPage <MainView>(clearNavigationHistory : true); }
public async Task <string> RefreshTokenAsync() { if (AuthenticateResult == null || string.IsNullOrWhiteSpace(AuthenticateResult.RefreshToken)) { throw new ApplicationException("No refresh token!"); } using (var client = CreateApiClient()) { var response = await client.Request(RefreshTokenUrlSegment) .PostUrlEncodedAsync(new { refreshToken = AuthenticateResult.RefreshToken }) .ReceiveJson <AjaxResponse <RefreshTokenResult> >(); if (!response.Success) { AuthenticateResult = null; throw new UserFriendlyException(response.Error.Message + ": " + response.Error.Details); } AuthenticateResult.AccessToken = response.Result.AccessToken; AccessTokenRetrieveTime = DateTime.Now; return(response.Result.AccessToken); } }
public async Task StoreAuthenticateResultAsync(AbpAuthenticateResultModel authenticateResultModel) { await _dataStorageManager.StoreAsync( DataStorageKey.CurrentSession_TokenInfo, _objectMapper.Map <AuthenticateResultPersistanceModel>(authenticateResultModel) ); }
//public async Task<string> GetAccessTokenAsync() //{ // if (_authenticateResult != null) // { // return await Task.FromResult(_authenticateResult.AccessToken) ; // } // throw new UnauthorizedAccessException(); //} public async Task <AbpAuthenticateResultModel> LoginAsync() { EnsureUserNameAndPasswordProvided(); using (IFlurlClient client = new FlurlClient(ApiUrlConfig.BaseUrl)) { client.AllowAnyHttpStatus(); client.WithHeader("Accept", new MediaTypeWithQualityHeaderValue("application/json")); client.WithHeader("User-Agent", AbpApiClient.UserAgent); client.WithHeader("Abp.TenantId", "1"); var response = await client .Request(LoginUrlSegment) .PostJsonAsync(_authenticateModel) .ReceiveJson <AjaxResponse <AbpAuthenticateResultModel> >(); if (!response.Success) { _authenticateResult = null; throw new UserFriendlyException(response.Error.Code, response.Error.Message, response.Error.Details); } _authenticateResult = response.Result; ConfigureTokenRefresh(response.Result); return(_authenticateResult); } }
private async Task AuthenticateSucceed(AbpAuthenticateResultModel result) { AuthenticateResultModel = result; if (AuthenticateResultModel.ShouldResetPassword) { dialog.ShowMessage("", Local.Localize("ChangePasswordToLogin")); return; } if (AuthenticateResultModel.RequiresTwoFactorVerification) { DialogParameters param = new DialogParameters(); param.Add("Value", AuthenticateResultModel); await dialog.ShowDialogAsync(AppViewManager.SendTwoFactorCode, param, AppCommonConsts.LoginIdentifier); } if (!AuthenticateModel.IsTwoFactorVerification) { await dataStorageService.StoreAuthenticateResultAsync(AuthenticateResultModel); } await SetCurrentUserInfoAsync(); await UserConfigurationManager.GetAsync(); }
public async Task <AbpAuthenticateResultModel> LoginAsync() { EnsureUserNameAndPasswordProvided(); using (var client = CreateApiClient()) { if (_applicationContext.CurrentTenant != null) { client.WithHeader(_multiTenancyConfig.TenantIdResolveKey, _applicationContext.CurrentTenant.TenantId); } var response = await client .Request(LoginUrlSegment) .PostJsonAsync(_authenticateModel) .ReceiveJson <AjaxResponse <AbpAuthenticateResultModel> >(); if (!response.Success || response.Result == null) { AuthenticateResult = null; throw new UserFriendlyException(response.Error.Message + ": " + response.Error.Details); } AuthenticateResult = response.Result; AuthenticateResult.RefreshTokenExpireDate = DateTime.Now.Add(AppConsts.RefreshTokenExpiration); return(AuthenticateResult); } }
private async Task AuthenticateSucceed(AbpAuthenticateResultModel result) { AuthenticateResultModel = result; if (AuthenticateResultModel.ShouldResetPassword) { await UserDialogs.Instance.AlertAsync(L.Localize("ChangePasswordToLogin"), L.Localize("LoginFailed"), L.Localize("Ok")); return; } if (AuthenticateResultModel.RequiresTwoFactorVerification) { //await _navigationService.SetMainPage<SendTwoFactorCodeView>(AuthenticateResultModel); return; } if (string.IsNullOrEmpty(AbpAuthenticateModel.TwoFactorVerificationCode)) { await SaveCredentialsAsync(result); } await SetCurrentUserInfoAsync(); //await UserConfigurationManager.GetAsync(); await UserConfigurationManager.GetIfNeedsAsync(); }
public async Task StoreAuthenticateResultAsync(AbpAuthenticateResultModel authenticateResultModel) { _dataStorageManager.SetValue( DataStorageKey.CurrentSession_TokenInfo, mapper.Map <AuthenticateResultPersistanceModel>(authenticateResultModel) ); await Task.CompletedTask; }
private async Task SaveCredentialsAsync(AbpAuthenticateResultModel result) { await _dataStorageManager.StoreAsync(DataStorageKey.Username, AbpAuthenticateModel.UserNameOrEmailAddress); await _dataStorageManager.StoreAsync(DataStorageKey.AccessToken, result.AccessToken); await _dataStorageManager.StoreAsync(DataStorageKey.RefreshToken, result.RefreshToken, true); await _dataStorageManager.StoreAsync(DataStorageKey.LastTokenRequestedTime, DateTime.UtcNow); if (_applicationContext.CurrentTenant != null) { await _dataStorageManager.StoreAsync(DataStorageKey.TenancyName, _applicationContext.CurrentTenant?.TenancyName); await _dataStorageManager.StoreAsync(DataStorageKey.TenantId, _applicationContext.CurrentTenant?.TenantId); } var applicationContext = this._applicationContext; if (applicationContext != null) { applicationContext.AppInformation = new AppInformation(); if (_dataStorageManager.HasKey(DataStorageKey.LastTokenRequestedTime)) { applicationContext.AppInformation.LastTokenRequestedTime = _dataStorageManager.Retrieve <DateTime>(DataStorageKey.LastTokenRequestedTime); } if (_dataStorageManager.HasKey(DataStorageKey.AccessToken)) { applicationContext.AppInformation.AccessToken = _dataStorageManager.Retrieve <string>(DataStorageKey.AccessToken); } if (_dataStorageManager.HasKey(DataStorageKey.RefreshToken)) { applicationContext.AppInformation.RefreshToken = _dataStorageManager.Retrieve <string>(DataStorageKey.RefreshToken, shouldDecrpyt: true); } } }
private void ConfigureTokenRefresh(AbpAuthenticateResultModel authenticateResult) { if (authenticateResult.ExpireInSeconds <= 1) { //Normally, ExpireInSeconds should never be that small. However, we wanted to handle the case. _refreshTimer?.Dispose(); _refreshTimer = null; } var reAuthenticationWaitTime = (authenticateResult.ExpireInSeconds - 1) * 1000; if (_refreshTimer == null) { _refreshTimer = new Timer(RefreshTimerElapsed, _authenticateModel, reAuthenticationWaitTime, reAuthenticationWaitTime); } else { _refreshTimer.Change(reAuthenticationWaitTime, reAuthenticationWaitTime); } }
public async Task <AbpAuthenticateResultModel> LoginAsync() { EnsureUserNameAndPasswordProvided(); using (IFlurlClient client = new FlurlClient(ApiUrlConfig.BaseUrl)) { client.WithHeader("Accept", new MediaTypeWithQualityHeaderValue("application/json")); client.WithHeader("User-Agent", AbpApiClient.UserAgent); if (AbpApiClient.TimeoutSeconds.HasValue) { client.WithTimeout(AbpApiClient.TimeoutSeconds.Value); } if (_applicationContext.CurrentTenant != null) { client.WithHeader(MultiTenancyConsts.TenantIdResolveKey, _applicationContext.CurrentTenant.TenantId); } var response = await client .Request(LoginUrlSegment) .PostJsonAsync(_authenticateModel) .ReceiveJson <AjaxResponse <AbpAuthenticateResultModel> >(); if (!response.Success) { _authenticateResult = null; throw new UserFriendlyException(response.Error.Message + ": " + response.Error.Details); } _authenticateResult = response.Result; ConfigureTokenRefresh(response.Result); return(_authenticateResult); } }
public void Logout() { _authenticateResult = null; }