Exemple #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"
        });
    }
Exemple #2
0
    public void Logout()
    {
        AuthServer.UserLoggingOut?.Invoke(ControllerContext, UserEntity.Current);

        UserTicketServer.RemoveCookie(ControllerContext);
    }
Exemple #3
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
        });
    }