Esempio n. 1
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = new User
                {
                    UserName   = model.UserName,
                    FirstName  = model.FirstName,
                    MiddleName = model.MiddleName,
                    LastName   = model.LastName
                };

                var result = await _userManager.CreateAsync(user, model.Password);

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

                    return(Ok(Jwt.GenerateTokenAsync(user, _configuration["JwtKey"], Convert.ToInt32(_configuration["JwtExpireDays"]), _configuration["JwtIssuer"], _configuration["JwtIssuer"])));
                }
                else
                {
                    return(NotFound());
                }
            }
            else
            {
                return(NotFound());
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(model.UserName, model.Password, false, false);

                if (result.Succeeded)
                {
                    var user = _userManager.Users.SingleOrDefault(r => r.NormalizedUserName == model.UserName.Normalize());
                    return(Ok(Jwt.GenerateTokenAsync(user, _configuration["JwtKey"], Convert.ToInt32(_configuration["JwtExpireDays"]), _configuration["JwtIssuer"], _configuration["JwtIssuer"])));
                }
            }
            else
            {
                return(NotFound());
            }

            return(NotFound());
        }