示例#1
0
        public async Task <IActionResult> SignIn(string Login, string Password)
        {
            try
            {
                Guid id = await userService.CheckUser(Login, Password);

                if (!(await tokenService.GetUserIdByToken(CookieController.GetOrGenerateToken(HttpContext))).Equals(id))
                {
                    await tokenService.AddToken(CookieController.GetOrGenerateToken(HttpContext), id);
                }
                return(RedirectToAction("Index", "Profile"));
            }
            catch (Exception e)
            {
                if (e is InvalidOperationException)
                {
                    ViewBag.Alert = "Неверный логин или пароль";
                    return(View());
                }
                else if (e is ArgumentNullException)
                {
                    ViewBag.Alert = "Одно или несколько полей пустые";
                    return(View());
                }
                else
                {
                    throw;
                }
            }
        }
        public async Task <ActionResult> Login(string userName, string password)
        {
            var user = await _userManager.FindByNameAsync(userName);

            var checkPassword = await _userManager.CheckPasswordAsync(user, password);

            if (!checkPassword)
            {
                return(ThrowJsonMessage(false, "用户名或密码错误"));
            }

            if (!await _userManager.IsEmailConfirmedAsync(user))
            {
                return(ThrowJsonMessage(false, "账户尚未激活"));
            }

            //生成token
            var token        = _tokenService.GenerateAccessToken(userName);
            var refreshToken = _tokenService.GenerateRefreshToken();

            var securityToken = new AspNetUserSecurityTokenDTO
            {
                UserName     = userName,
                RefreshToken = refreshToken
            };
            var isSave = _tokenService.AddToken(securityToken);

            if (isSave)
            {
                _tokenService.SetAccessTokenToCache(userName, token);//缓存token
                return(new ObjectResult(new { success = true, token = token, refreshToken = refreshToken }));
            }

            return(BadRequest());
        }
示例#3
0
        public JsonResult SaveToken(Token modal)
        {
            Patient patient = new Patient();

            patient = _patientService.GetPatientByPatientNo(modal.PatientNo.Split('(')[0]);
            if (patient == null)
            {
                return(new JsonResult {
                    Data = new { Status = "No Patient" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
            else
            {
                modal.PatientId     = patient.PatientId;
                modal.GeneratedTime = DateTime.Now;
                modal.IsAnnounced   = false;
                modal.StatusId      = 1;
                _tokenService.AddToken(modal);
                return(new JsonResult {
                    Data = new { Status = "Success" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }