コード例 #1
0
        /// <summary>
        /// Logins the action.
        /// </summary>
        private async Task LoginAction()
        {
            var loginRequest = new BaseFacadeRequestModelWithContent <LoginRequestModel>
            {
                Content = new LoginRequestModel
                {
                    ApplicationUsesRefreshTokens = true,
                    Email    = this.Username,
                    Password = this.Password
                }
            };

            // ================ Just for Testing without Api call =====================

            //var identity = await this.authenticationFacade.LoginAsync(this.Username, this.Password);

            //// if success navigate to HomePage
            //// if not display message
            //if (identity.IsValidUser)
            //{
            //	this.navigationService.NavigateAsync("MainMasterDetailPage/MainNavigationPage/HomePage").Forget();
            //}
            //else
            //{
            //	// Dialog service show login error.
            //	await this.DialogService.DisplayAlertAsync(Constants.LoginErrorMessageTitle, Constants.LoginErrorMessage, Constants.LoginErrorMessageOkButton);
            //}

            // =============================== End  =============================================


            // authenticate
            var loginResponse = await this.authenticationFacade.Login(loginRequest);

            // if success navigate to HomePage
            // if not display message
            if (loginResponse.StatusCode == HttpStatusCode.OK)
            {
                //shahbaaz 05/07/2017: changed URI from relative to absolute for clearning nav stack
                this.navigationService.NavigateAsync("app:///MainMasterDetailPage/Navigation/HomePage/DashboardPage").Forget();

                // this.navigationService.NavigateAsync("MainMasterDetailPage/Navigation/HomePage/DashboardPage").Forget();
            }
            else
            {
                // Dialog service show login error.
                await this.DialogService.DisplayAlertAsync(Constants.LoginErrorMessageTitle, Constants.LoginErrorMessage, Constants.LoginErrorMessageOkButton);
            }
        }
コード例 #2
0
        public async Task <BaseFacadeResponseModel <LoginResponseModel> > Login(BaseFacadeRequestModelWithContent <LoginRequestModel> model)
        {
            var apiRequestModel = Mapper.Map <LoginApiRequestModel>(model.Content);

            var authResponse = await _authenticationHelperWrapper.GetUserToken(model.Content.Email, model.Content.Password, model.Content.ApplicationUsesRefreshTokens);

            if (authResponse == null || authResponse.IsError)
            {
                //log unsuccessful login attempt
                var addLogEntryApiRequestModel = new AddLogEntryApiRequestModel
                {
                    StatusCode = 404,
                    Date       = DateTime.Now,
                    Type       = "Login attempt",
                    IpAddress  = model.Content.IpAddress,
                    Message    = "A failed login attempt occurred using the email address " + model.Content.Email
                };

                //Todo: log addLogEntryApiRequestModel

                if (authResponse != null)
                {
                    return(new BaseFacadeResponseModel <LoginResponseModel>
                    {
                        //authResponse.Json.GetValue("error_description");
                        Message =
                            authResponse.Json.GetValue("error_description")
                            .ToString(),     //"The username or password entered was incorrect. Please check your details and try again.",
                        StatusCode = System.Net.HttpStatusCode.Unauthorized
                    });
                }
            }

            var runner = await _authenticationHelperWrapper.TraverseReauth(() => _authApi.Login(authResponse.AccessToken, apiRequestModel), authResponse.RefreshToken);

            //generic helper that sets up the return object
            var runnerResponse = await new ApiRunnerResponseHelper().ReturnRunnerResponse(runner, new LoginResponseModel(), new LoginApiResponseModel());

            var mappedFacadeResponse = (runnerResponse.FacadeResponseModel as BaseFacadeResponseModel <LoginResponseModel>);

            mappedFacadeResponse.AccessToken  = authResponse.AccessToken;
            mappedFacadeResponse.RefreshToken = authResponse.RefreshToken ?? string.Empty;

            //here we need to translate the api content response into the UI response
            if (runnerResponse.ApiContentResponseModelContent != null)
            {
                var apiResponse = (runnerResponse.ApiContentResponseModelContent as LoginApiResponseModel);
                mappedFacadeResponse.Content = new LoginResponseModel
                {
                    UserDetails       = apiResponse.UserDetails,
                    VerificationToken = apiResponse.VerificationToken,
                    UserId            = apiResponse.UserDetails.UserId
                };
            }
            else
            {
                //log unsuccessful login attempt
                var addLogEntryApiRequestModel = new AddLogEntryApiRequestModel
                {
                    StatusCode = 404,
                    Date       = DateTime.Now,
                    Type       = "Login attempt",
                    IpAddress  = model.Content.IpAddress,
                    Message    = "A failed login attempt occurred using the email address " + model.Content.Email
                };

                //Todo: reinstate this log entry
                //await _logApi.AddLogEntry();
            }

            var Credentials = new KeychainModel()
            {
                Username              = model.Content.Email,
                Password              = model.Content.Password,
                AccessToken           = mappedFacadeResponse.AccessToken,
                RefreshToken          = mappedFacadeResponse.RefreshToken,
                AccessTokenExpireDate = DateTime.Now.AddMinutes(20),
                UserId = mappedFacadeResponse.Content.UserId
            };

            this.keychainService.SaveCredentials(Credentials);

            return(mappedFacadeResponse);
        }