public async Task <AuthorizationResultModel> RegisterAsync(UserModel model)
        {
            User user   = _mapper.Map <UserModel, User>(model);
            var  result = await _userManager.CreateAsync(user, model.Password);

            AuthorizationResultModel registrationResult = new AuthorizationResultModel();

            if (result.Succeeded)
            {
                string role = "user";
                await _userManager.AddToRoleAsync(user, role);

                await _signInManager.SignInAsync(user, false);

                registrationResult.IsSuccessful = true;
                registrationResult.Token        = await _tokenService.GetToken(user);

                registrationResult.UserName = model.UserName;
                registrationResult.Role     = role;

                return(registrationResult);
            }
            foreach (var error in result.Errors)
            {
                registrationResult.Errors.Add(error.Description);
            }
            return(registrationResult);
        }
Beispiel #2
0
        public AuthorizationResultModel Authorize()
        {
            _currentUser = AuthHelper.GetUserFromAuthToken(Request.Headers?.Authorization?.Parameter);
            if (_currentUser is null)
            {
                return(null);
            }

            var model = new AuthorizationResultModel
            {
                Email        = _currentUser.Email,
                IsAuthorized = false,
                Role         = UserRole.Null
            };

            _currentUser = AuthHelper.GetUserFromAuthHeader(Request);
            if (_currentUser is null)
            {
                return(model);
            }
            model.IsAuthorized = _currentUser.IsApproved;
            model.Role         = (UserRole)_currentUser.Role;
            model.FirstName    = _currentUser.FirstName;
            model.LastName     = _currentUser.LastName;
            return(model);
        }
Beispiel #3
0
        public async Task <IActionResult> Post([FromBody] LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                UserModel user = _mapper.Map <LoginViewModel, UserModel>(model);
                AuthorizationResultModel result = await _userService.LoginAsync(user);

                if (result.IsSuccessful)
                {
                    return(Ok(new { token = result.Token, userName = result.UserName, role = result.Role }));
                }
                return(BadRequest(result.Errors));
            }
            return(BadRequest(ModelState));
        }
Beispiel #4
0
        public async Task <IActionResult> LoginAsync(LoginRequest loginRequest)
        {
            LoginModel loginModel = loginRequest.Adapt <LoginModel>();

            AuthorizationResultModel result = await _accountService.AuthorizeUserAsync(loginModel);

            if (result.ResultStatus == AuthorizationResultStatus.Ok)
            {
                AuthorizationResponse authorizationResponse = result.Adapt <AuthorizationResponse>();

                await HttpContext.SignInAsync(authorizationResponse);

                return(Ok(authorizationResponse));
            }

            return(Unauthorized("Incorrect login data"));
        }
        public async Task <AuthorizationResultModel> LoginAsync(UserModel model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);

            AuthorizationResultModel loginResult = new AuthorizationResultModel();

            if (result.Succeeded)
            {
                User user = await _userManager.FindByNameAsync(model.UserName);

                loginResult.Token = await _tokenService.GetToken(user);

                loginResult.IsSuccessful = true;
                loginResult.UserName     = model.UserName;
                loginResult.Role         = (await _userManager.GetRolesAsync(user)).First();

                return(loginResult);
            }
            loginResult.Errors.Add("Invalid username or password");
            return(loginResult);
        }