Example #1
0
        public async Task <IActionResult> SignIn([FromBody] LoginDto loginDto)
        {
            var token = tokenProvider.GenerateToken(loginDto.Username, loginDto.Password);
            var user  = userRepositoryService.FindUserByUsername(loginDto.Username);

            if (token == null)
            {
                return(NotFound("Bad username or password"));
            }
            token.User = user;

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.NameIdentifier, user.Id),
                new Claim(ClaimTypes.Role, user.Role.Name),
            };

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var principal = new ClaimsPrincipal(new[] { claimsIdentity });
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

            return(Json(token));
        }
Example #2
0
        public IActionResult Authorize([FromBody] LoginDto loginDto)
        {
            var token = tokenProvider.GenerateToken(loginDto.Username, loginDto.Password);
            var user  = userRepositoryService.FindUserByUsername(loginDto.Username);

            if (token == null)
            {
                return(NotFound("Bad username or password"));
            }
            // TODO: Create better way to handle adding user to token
            token.User = user;

            return(Json(token));
        }