Exemple #1
0
        protected virtual async void DoSignIn()
        {
            await Task.Run(async() =>
            {
                try
                {
                    var user   = UserName?.Trim().ToLower();
                    var pass   = Password?.Trim();
                    IsBusy     = true;
                    var result = await AuthApi.SignIn(user, pass);
                    IsBusy     = false;

                    if (result.Result == CognitoResult.Ok)
                    {
                        SessionStore.UserName           = user;
                        SessionStore.AccessToken        = result.AccessToken;
                        SessionStore.IdToken            = result.IdToken;
                        SessionStore.RefreshToken       = result.RefreshToken;
                        SessionStore.SessionId          = result.SessionId;
                        SessionStore.TokenIssuedServer  = result.TokenIssued;
                        SessionStore.TokenExpiresServer = result.Expires;

                        await OnAuthenticated();
                    }
                    else if (result.Result == CognitoResult.NotAuthorized)
                    {
                        await OnNotAuthorized();
                    }
                    else if (result.Result == CognitoResult.NotConfirmed)
                    {
                        await OnConfirmationRequired();
                    }
                    else if (result.Result == CognitoResult.UserNotFound)
                    {
                        await OnNoSuchUser();
                    }
                    else if (result.Result == CognitoResult.PasswordChangeRequred)
                    {
                        SessionStore.UserName           = user;
                        SessionStore.AccessToken        = result.AccessToken;
                        SessionStore.IdToken            = result.IdToken;
                        SessionStore.RefreshToken       = result.RefreshToken;
                        SessionStore.SessionId          = result.SessionId;
                        SessionStore.TokenIssuedServer  = result.TokenIssued;
                        SessionStore.TokenExpiresServer = result.Expires;

                        await OnPasswordChangeRequired();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Exception in {this.GetType().Name} {e.GetType().Name}:{e.Message}");
                }
            });
        }
Exemple #2
0
        private async Task <SignInDto> ActualSignIn(string lowerCaseUsername, string passwordToken)
        {
            HttpResponseMessage response;

            try
            {
                _        = _logger.Log("API LOGIN");
                response = await _api.SignIn(lowerCaseUsername, passwordToken);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("timeout"))
                {
                    throw new Timeout();
                }
                throw;
            }

            if (response.IsSuccessStatusCode)
            {
                var signinDto = JsonConvert.DeserializeObject <SignInDto>(await response.Content.ReadAsStringAsync());
                signinDto.Session.Username = lowerCaseUsername;
                return(signinDto);
            }

            var one = ParseGenericErrors(await response.Content.ReadAsStringAsync(), response.StatusCode);

            if (one != null)
            {
                throw one;
            }

            var two = await ParseGenericUsernamePasswordError(response);

            if (two != null)
            {
                throw two;
            }

            if (await response.Content.ReadAsStringAsync() == "Invalid password")
            {
                throw new UsernameOrPasswordMismatch();
            }

            throw new Exception($"Unknown error during SignIn: {response.StatusCode}");
        }
        public IActionResult LogIn(int userId, string userPassword)
        {
            if (!String.IsNullOrEmpty(userPassword))
            {
                User user = authApi.SignIn(userId, userPassword);
                if (user != null)
                {
                    setUserSession(user.Id.Value, user.RoleId.Value);
                    return(RedirectToAction("Profile", "Users", new { id = userId }));
                }
                else
                {
                    ViewData["Message"] = "user id or password is incorrect";
                }
            }

            putSessionToViewData();
            return(View());
        }