예제 #1
0
        public async Task <IActionResult> LogOn(LogonModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await _accountService.LogOn(model.Username,
                                                         model.Password);

                if (result.IsSuccess)
                {
                    var logonDetail = JwtTokenUtil.GetLogonDetail(result.Data.AccessToken);

                    if (logonDetail != null)
                    {
                        var claims = new List <Claim>
                        {
                            new Claim(ClaimTypes.NameIdentifier,
                                      logonDetail.Id.ToString()),
                            new Claim(ClaimTypes.Name,
                                      logonDetail.DisplayName),
                            new Claim(ClaimTypes.GivenName,
                                      logonDetail.Username),
                            new Claim("avatar",
                                      logonDetail.Avatar),
                            new Claim("Token",
                                      result.Data.AccessToken)
                        };
                        var identity = new ClaimsIdentity(claims,
                                                          "cookie");
                        var principal = new ClaimsPrincipal(identity);
                        await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
                                                      principal,
                                                      new AuthenticationProperties
                        {
                            IsPersistent = model.RememberMe,
                            ExpiresUtc   = DateTime.UtcNow.AddDays(365)
                        });

                        if (string.IsNullOrEmpty(model.ReturnUrl))
                        {
                            return(RedirectToAction("Index",
                                                    "Home"));
                        }

                        return(RedirectToAction(model.ReturnUrl));
                    }

                    ModelState.AddModelError("", ErrorEnum.AUTHENTICATION_WRONG.GetStringValue());
                }

                ModelState.AddModelError("",
                                         result.Message);
            }

            return(View(model));
        }