예제 #1
0
        public async Task <MessageModel <dynamic> > RefrenshToken(string token = "")
        {
            var data = new MessageModel <dynamic>();

            if (token.IsNullOrEmpty())
            {
                data.msg = "令牌无效,重新登录";
                return(data);
            }
            var jwt  = JwtHelper.SerializeJwt(token);
            var Info = await _userserver.GetSysUser(jwt.Uid);

            if (jwt.Uid != Guid.Empty && Info != null)
            {
                var RoleName = await _userserver.GetUserRoleByUserId(Info.Id);

                var claims = new List <Claim>
                {
                    new Claim(ClaimTypes.Name, Info.Name),                                                                      //设置用户名称
                    new Claim(JwtRegisteredClaimNames.Jti, Info.Id.ToString()),                                                 //设置用户ID
                    new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString()), //设置过期时间
                    new Claim("Id", Info.Id.ToString()),
                    new Claim("userName", Info.Name)
                };
                claims.AddRange(RoleName.Split(',').Select(x => new Claim(ClaimTypes.Role, x))); //将用户角色填充到claims中
                var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);       //用户标识
                identity.AddClaims(claims);
                var tokenkey = JwtToken.BuildJwtToken(claims.ToArray(), _requirement);
                data.response = tokenkey;
                data.msg      = "refrenshToken success! loading data...";
                data.success  = true;
                return(data);
            }
            return(data);
        }
예제 #2
0
        public async Task <MessageModel <SysUser> > GetUser(string token)
        {
            var ss   = HttpContext.User.Claims;
            var data = new MessageModel <SysUser>();

            if (!token.IsNullOrEmpty())
            {
                //解析Token字符串,获取到用户ID
                var tokenModel = JwtHelper.SerializeJwt(token);
                if (tokenModel != null && tokenModel.Uid != Guid.Empty)
                {
                    //根据用户ID获取用户信息
                    var userInfo = await _userserver.GetSysUser(tokenModel.Uid);

                    if (userInfo != null)
                    {
                        data.response = userInfo;
                        data.success  = true;
                        data.msg      = "用户信息获取成功";
                    }
                }
            }
            return(data);
        }