Пример #1
0
        /// <summary>
        /// 刷新token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public dynamic UpdateToken(string token)
        {
            JwtSecurityToken readtoken = new JwtSecurityTokenHandler().ReadJwtToken(token);

            //加入黑名单
            if (!_cacheService.Exists(readtoken.Payload["ID"].ToString()))
            {
                _cacheService.Add(readtoken.Payload["ID"].ToString(), token);
            }

            DateTime expirteTime = DateTime.UtcNow.AddMinutes(Convert.ToDouble(ConfigHelper.GetSectionValue("expiresAt")));
            Dictionary <string, object> payload = new Dictionary <string, object>();

            payload.Add("ID", readtoken.Payload["ID"]);
            payload.Add("UserName", readtoken.Payload["UserName"]);
            payload.Add("RolesID", readtoken.Payload["RolesID"]);
            payload.Add("Email", readtoken.Payload["Email"]);

            var tokenacces = new
            {
                UserId      = readtoken.Payload["ID"],
                AccessToken = Encrypts.CreateToken(payload, Convert.ToInt32(ConfigHelper.GetSectionValue("expiresAt"))),
                Expires     = new DateTimeOffset(expirteTime).ToUnixTimeSeconds(),
                Success     = true
            };

            return(tokenacces);
        }
Пример #2
0
        /// <summary>
        /// 创建Token值
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回token 数据</returns>
        public dynamic CreateToken(UserEntity entity)
        {
            DateTime expirteTime = DateTime.UtcNow.AddMinutes(Convert.ToDouble(ConfigHelper.GetSectionValue("expiresAt")));
            Dictionary <string, object> payload = new Dictionary <string, object>();

            payload.Add("ID", entity.ID);
            payload.Add("UserName", entity.UserName);
            payload.Add("Email", entity.Email);
            payload.Add("RolesID", entity.RoleID);
            var tokenacces = new
            {
                UserId = entity.ID,
                //RolesID=entity.RoleID,
                entity.UserName,
                AccessToken = Encrypts.CreateToken(payload, Convert.ToInt32(ConfigHelper.GetSectionValue("expiresAt"))),
                Expires     = new DateTimeOffset(expirteTime).ToUnixTimeSeconds(),
                Success     = true
            };

            if (tokenacces.Success)
            {
                _cacheService.Add(entity.ID, tokenacces.AccessToken);
            }
            return(tokenacces);
        }
Пример #3
0
        public JObject Token1([FromBody] Post_UserViewModel obj)
        {
            DataResult result = new DataResult();

            result.verifiaction = false;
            try
            {
                string name     = obj.name;
                string password = obj.password;
                if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
                {
                    result.message = "账号或者密码不能为空!";
                    return(JObject.FromObject(result));
                }

                var entity = _userRepsonsityService.Login(name, password);

                if (entity != null)
                {
                    Dictionary <string, object> payload = new Dictionary <string, object>();
                    payload.Add("ID", entity.ID);
                    payload.Add("UserName", entity.UserName);
                    payload.Add("Email", entity.Email);

                    var tokenacces = new
                    {
                        AccessToken = Encrypts.CreateToken(payload, 30),
                        Expires     = 3600
                    };
                    result.rows         = tokenacces;
                    result.verifiaction = true;
                    result.message      = "登陆成功!";
                }
                else
                {
                    result.message      = "获取token令牌失败!";
                    result.verifiaction = true;
                }
            }
            catch (Exception ex)
            {
                result.message = "非法登陆!";
                return(JObject.FromObject(result));
            }
            finally
            {
            }
            return(JObject.FromObject(result));
        }