Esempio n. 1
0
        public async Task <IActionResult> Login([FromBody] LoginDTO model)
        {
            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);

            if (result.Succeeded)
            {
                var user  = _userManager.Users.SingleOrDefault(r => r.Email == model.Email);
                var token = await AuthenticationUtils.GenerateJwtToken(model.Email, user, _configuration["JwtKey"], _configuration["JwtExpireDays"], _configuration["JwtIssuer"]);

                return(Ok(token));
            }
            return(Unauthorized("Incorrect credentials provided"));
        }
Esempio n. 2
0
        public async Task <IActionResult> Register([FromBody] RegisterDTO model)
        {
            var user = new IdentityUser
            {
                UserName = model.Email,
                Email    = model.Email
            };
            var result = await _userManager.CreateAsync(user, model.Password);

            if (result.Succeeded)
            {
                await _signInManager.SignInAsync(user, false);

                var token = await AuthenticationUtils.GenerateJwtToken(model.Email, user, _configuration["JwtKey"], _configuration["JwtExpireDays"], _configuration["JwtIssuer"]);

                return(Ok(token));
            }
            return(BadRequest(result.Errors));
        }