示例#1
0
        public async Task <IActionResult> GetUserByTokens()
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var tokenResponse = await _tokensService.CreateTokenAsync(User);

            if (tokenResponse == null)
            {
                return(BadRequest("User with such Uid not registered yet!"));
            }

            return(Ok(tokenResponse));
        }
        public async Task <object> CreateTokenAsync([FromHeader] string userName, [FromHeader] string password)
        {
            var user = await _usersService.GetByNameAsync(
                userName, new Expression <Func <User, object> >[]
            {
                x => x.Email,
                x => x.Group,
                x => x.UserName,
                x => x.UserType,
                x => x.Id
            });

            var token = await _tokensService.CreateTokenAsync(user.Id, password)
                        ?? throw new UnauthorizedAccessException();

            return(new
            {
                Token = token,
                User = user
            });
        }