コード例 #1
0
        public async Task <IActionResult> LoginUser([FromBody] UserLoginModel loginModel)
        {
            if (string.IsNullOrEmpty(loginModel.Username) || string.IsNullOrEmpty(loginModel.Password))
            {
                return(BadRequest("Invalid model"));
            }

            var profile = await _userManager.GetByLogin(loginModel.Username);

            if (profile != null)
            {
                var user = _authorizationManager.Authenticate(loginModel, profile);

                if (user != null)
                {
                    var result      = GenerateToken(profile);
                    var tokenString = new JwtSecurityTokenHandler().WriteToken(result);
                    var response    = new TokenResponse
                    {
                        Token        = tokenString,
                        TokenExpires = result.ValidTo,
                        UserId       = user.Id
                    };

                    return(Ok(response));
                }
                return(Unauthorized());
            }

            return(BadRequest("Profile with give login not found"));
        }
コード例 #2
0
        public async Task <IActionResult> LoginUser([FromBody] UserLoginModel loginModel)
        {
            if (string.IsNullOrEmpty(loginModel.Username) || string.IsNullOrEmpty(loginModel.Password))
            {
                return(BadRequest("Invalid model"));
            }

            var profile = await _userManager.GetByLogin(loginModel.Username);

            if (profile != null)
            {
                var user = _authorizationManager.Authenticate(loginModel, profile);

                if (user != null)
                {
                    var result = GenerateToken(profile);
                    return(Ok(new { User = user.Id, Token = result }));
                }
                return(Unauthorized());
            }

            return(NotFound("Profile with give login not found"));
        }