public async Task <IActionResult> OnPostLogin(string login, string password, bool remember)
        {
            await _authentication.AuthenticateAsync(new UserData(login, _dataHandler.GetMD5Hash(password)), remember);

            if (_authentication.Authenticated)
            {
                return(RedirectToPage("/Index"));
            }

            return(RedirectToPage("/Login"));
        }
Beispiel #2
0
        public async Task <IActionResult> OnPostChangeUser(string phone, string email, string old_password, string new_password, string repeat_password)
        {
            try
            {
                var dataCorrectnessFlag = _dataHandler.IsEmailValid(email) && _dataHandler.IsPhoneValid(phone);
                if (old_password != null && new_password != null && repeat_password != null &&
                    _dataHandler.GetMD5Hash(old_password) == _authentication.User.Password && new_password == repeat_password && dataCorrectnessFlag)
                {
                    var oldUser = await _unitOfWork.UsersRepository.GetByLoginAsync(_authentication.User.Login);

                    var user = new Users()
                    {
                        phone    = phone,
                        email    = email,
                        password = _dataHandler.GetMD5Hash(repeat_password),
                        role     = oldUser.role,
                        status   = oldUser.status,
                    };
                    await _unitOfWork.UsersRepository.UpdateAsync(user, oldUser);

                    _authentication.RemoveAuthentication();
                    await _authentication.AuthenticateAsync(new UserData(oldUser.login, user.password));

                    return(RedirectToPage());
                }

                else if (old_password == null && new_password == null && repeat_password == null && dataCorrectnessFlag)
                {
                    var oldUser = await _unitOfWork.UsersRepository.GetByLoginAsync(_authentication.User.Login);

                    var user = new Users()
                    {
                        phone  = phone,
                        email  = email,
                        status = oldUser.status,
                        role   = oldUser.role,
                    };
                    await _unitOfWork.UsersRepository.UpdateAsync(user, oldUser);

                    return(RedirectToPage());
                }
                else
                {
                    return(RedirectToPage());
                }
            }
            catch
            {
                return(RedirectToPage());
            }
        }
 public LoginViewModel(IAuthentication authentication = null)
 {
     authentication = authentication ?? new Authentication();
     var canLogin = this.WhenAny(x => x.LoginName,
         x => x.Password,
         (l, p) => !String.IsNullOrWhiteSpace(l.Value) && !String.IsNullOrWhiteSpace(p.Value));
     LoginCommand = new ReactiveCommand(canLogin);
     var loggedIn = LoginCommand.RegisterAsyncTask(_ => authentication.AuthenticateAsync(LoginName,
         Password).
                                                                       ContinueWith(t => t.Result == AuthenticationResult.Authenticated
                                                                                             ? "Přihlášen"
                                                                                             : "Nepřihlášen"));
     message = new ObservableAsPropertyHelper<string>(loggedIn,
         s => raisePropertyChanged("Message"));
 }
        public async Task <IActionResult> PostAsync([FromBody] LoginCredencials login)
        {
            var authentication = await _authenticate.AuthenticateAsync(login);

            if (!authentication.IsValid)
            {
                return(Unauthorized(authentication));
            }
            var authorization = await _authorize.AuthorizeAsync(authentication.DataEntity);

            if (!authorization.IsAuthorized)
            {
                return(Forbid());
            }
            return(Ok(authorization));
        }
Beispiel #5
0
        public async Task <ActionResult <Token> > AuthenticateAsync([FromHeader] string authorization)
        {
            if (authorization == null)
            {
                return(ErrorResponse.Failure <Token>(ErrorCodes.MissingCredentials, "Missing username or password"));
            }

            try
            {
                AuthorizationHeaderDecoder decoder          = new AuthorizationHeaderDecoder(authorization);
                Tuple <string, string>     usernamePassword = decoder.Decode();

                Token token = await authentication.AuthenticateAsync(usernamePassword.Item1, usernamePassword.Item2);

                return(token);
            }
            catch (Exception e)
            {
                return(ErrorResponse.Failure <Token>(ErrorCodes.GeneralFailure, e.Message));
            }
        }
Beispiel #6
0
        public async Task InvokeAsync(HttpContext context, IAuthentication authentication)
        {
            await authentication.AuthenticateAsync();

            await _next.Invoke(context);
        }