Esempio n. 1
0
        public bool ModifyToken(AspNetUserSecurityTokenDTO model)
        {
            Expression <Func <AspNetUserSecurityToken, bool> > expression = o => !o.IsDeleted;

            if (!string.IsNullOrEmpty(model.ClientId))
            {
                expression = expression.And(o => o.ClientId == model.ClientId);
            }

            if (!string.IsNullOrEmpty(model.UserName))
            {
                expression = expression.And(o => o.UserName == model.UserName);
            }

            var securityToken = _securityTokenRepository.Find(expression);

            securityToken.ToList().ForEach(o =>
            {
                if (!string.IsNullOrEmpty(model.RefreshToken))
                {
                    o.RefreshToken = model.RefreshToken;
                }
                o.IsRevoked = model.IsRevoked;
            });
            return(_securityTokenRepository.Modify(securityToken, new string[] { "RefreshToken", "IsRevoked" }) > 0);
        }
        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());
        }
Esempio n. 3
0
        public IActionResult RevokeToken()
        {
            AspNetUserSecurityTokenDTO model = new AspNetUserSecurityTokenDTO
            {
                UserName  = User.Identity.Name,
                IsRevoked = true
            };
            var result = _tokenService.ModifyToken(model);

            return(ThrowJsonMessage(result));
        }
Esempio n. 4
0
        public bool AddToken(AspNetUserSecurityTokenDTO model)
        {
            var securityToken = model.Map <AspNetUserSecurityToken>();

            return(_securityTokenRepository.Add(securityToken));
        }