Exemplo n.º 1
0
        public LoginResponse?LoginFromCookie()
        {
            if (!UserTicketServer.LoginFromCookie(ControllerContext))
            {
                return(null);
            }

            var token = AuthTokenServer.CreateToken(UserEntity.Current);

            return(new LoginResponse {
                userEntity = UserEntity.Current, token = token, authenticationType = "cookie"
            });
        }
Exemplo n.º 2
0
        public LoginResponse LoginFromCookie()
        {
            using (ScopeSessionFactory.OverrideSession())
            {
                if (!UserTicketServer.LoginFromCookie())
                {
                    return(null);
                }

                string message = AuthLogic.OnLoginMessage();

                var token = AuthTokenServer.CreateToken(UserEntity.Current);

                return(new LoginResponse {
                    message = message, userEntity = UserEntity.Current, token = token
                });
            }
        }
Exemplo n.º 3
0
        public LoginResponse?LoginFromCookie()
        {
            using (ScopeSessionFactory.OverrideSession())
            {
                if (!UserTicketServer.LoginFromCookie(ControllerContext))
                {
                    return(null);
                }

                string?message = AuthLogic.OnLoginMessage();

                var token = AuthTokenServer.CreateToken(UserEntity.Current);

                return(new LoginResponse {
                    message = message, userEntity = UserEntity.Current, token = token, authenticationType = "cookie"
                });
            }
        }
Exemplo n.º 4
0
        public LoginResponse Login([FromBody] LoginRequest data)
        {
            if (string.IsNullOrEmpty(data.userName))
            {
                throw ModelException("userName", AuthMessage.UserNameMustHaveAValue.NiceToString());
            }

            if (string.IsNullOrEmpty(data.password))
            {
                throw ModelException("password", AuthMessage.PasswordMustHaveAValue.NiceToString());
            }

            // Attempt to login
            UserEntity user = null;

            try
            {
                user = AuthLogic.Login(data.userName, Security.EncodePassword(data.password));
            }
            catch (Exception e) when(e is IncorrectUsernameException || e is IncorrectPasswordException)
            {
                if (AuthServer.MergeInvalidUsernameAndPasswordMessages)
                {
                    ModelState.AddModelError("userName", AuthMessage.InvalidUsernameOrPassword.NiceToString());
                    ModelState.AddModelError("password", AuthMessage.InvalidUsernameOrPassword.NiceToString());
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState));
                }
                else if (e is IncorrectUsernameException)
                {
                    throw ModelException("userName", AuthMessage.InvalidUsername.NiceToString());
                }
                else if (e is IncorrectPasswordException)
                {
                    throw ModelException("password", AuthMessage.InvalidPassword.NiceToString());
                }
            }
            catch (IncorrectPasswordException)
            {
                throw ModelException("password", AuthServer.MergeInvalidUsernameAndPasswordMessages ?
                                     AuthMessage.InvalidUsernameOrPassword.NiceToString() :
                                     AuthMessage.InvalidPassword.NiceToString());
            }

            using (UserHolder.UserSession(user))
            {
                if (data.rememberMe == true)
                {
                    UserTicketServer.SaveCookie();
                }

                AuthServer.AddUserSession(user);

                string message = AuthLogic.OnLoginMessage();

                var token = AuthTokenServer.CreateToken(user);

                return(new LoginResponse {
                    message = message, userEntity = user, token = token
                });
            }
        }
Exemplo n.º 5
0
        public void Logout()
        {
            AuthServer.UserLoggingOut?.Invoke();

            UserTicketServer.RemoveCookie();
        }
Exemplo n.º 6
0
        public ActionResult <LoginResponse> Login([Required, FromBody] LoginRequest data)
        {
            if (string.IsNullOrEmpty(data.userName))
            {
                return(ModelError("userName", LoginAuthMessage.UserNameMustHaveAValue.NiceToString()));
            }

            if (string.IsNullOrEmpty(data.password))
            {
                return(ModelError("password", LoginAuthMessage.PasswordMustHaveAValue.NiceToString()));
            }

            string authenticationType;
            // Attempt to login
            UserEntity user;

            try
            {
                if (AuthLogic.Authorizer == null)
                {
                    user = AuthLogic.Login(data.userName, Security.EncodePassword(data.password), out authenticationType);
                }
                else
                {
                    user = AuthLogic.Authorizer.Login(data.userName, data.password, out authenticationType);
                }
            }
            catch (Exception e) when(e is IncorrectUsernameException || e is IncorrectPasswordException)
            {
                if (AuthServer.MergeInvalidUsernameAndPasswordMessages)
                {
                    return(ModelError("login", LoginAuthMessage.InvalidUsernameOrPassword.NiceToString()));
                }
                else if (e is IncorrectUsernameException)
                {
                    return(ModelError("userName", LoginAuthMessage.InvalidUsername.NiceToString()));
                }
                else if (e is IncorrectPasswordException)
                {
                    return(ModelError("password", LoginAuthMessage.InvalidPassword.NiceToString()));
                }
                throw;
            }
            catch (Exception e)
            {
                return(ModelError("login", e.Message));
            }

            AuthServer.OnUserPreLogin(ControllerContext, user);

            AuthServer.AddUserSession(ControllerContext, user);

            if (data.rememberMe == true)
            {
                UserTicketServer.SaveCookie(ControllerContext);
            }

            var token = AuthTokenServer.CreateToken(user);

            return(new LoginResponse {
                userEntity = user, token = token, authenticationType = authenticationType
            });
        }
Exemplo n.º 7
0
        public void Logout()
        {
            AuthServer.UserLoggingOut?.Invoke(ControllerContext, UserEntity.Current);

            UserTicketServer.RemoveCookie(ControllerContext);
        }