예제 #1
0
        public async Task <IActionResult> Login(FrontEndLogin model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            else
            {
                var user = await _wpbService.AuthenticateAsync(model.Username, model.Password);

                if (user == null)
                {
                    ModelState.AddModelError(string.Empty, "Username or password was incorrect");
                    return(View(model));
                }
                else
                {
                    var claims = await BuildClaimsAsync(user);

                    var userIdentity = new ClaimsIdentity(claims, "login");
                    var principal    = new ClaimsPrincipal(userIdentity);
                    await HttpContext.SignInAsync(
                        CookieAuthenticationDefaults.AuthenticationScheme,
                        principal,
                        //  https://tahirnaushad.com/2017/09/08/asp-net-core-2-0-cookie-authentication/
                        new AuthenticationProperties
                    {
                        ExpiresUtc = DateTime.UtcNow.AddDays(30)
                    });

                    return(returnUrl != null?RedirectToLocal(returnUrl) : RedirectToAction("Index", "Home"));
                }
            }
        }
예제 #2
0
        public async Task <IActionResult> CreateToken([FromBody] LoginModelDto login)
        {
            var user = await _wpbService.AuthenticateAsync(login.Username, login.Password);

            if (user == null)
            {
                return(BadRequest(new ApiError(400, "BadRequest", "Username or password was incorrect.")));
            }

            var tokenString = GenerateToken(user);

            return(Ok(
                       new
            {
                Id = user.Id.ToString(),
                user.Username,
                user.FirstName,
                user.Email,
                Token = tokenString
            }));
        }