Exemplo n.º 1
0
        public async Task <object> GetJwtToken3(string name = "", string pass = "")
        {
            string jwtStr = string.Empty;

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pass))
            {
                return(new JsonResult(new
                {
                    Status = false,
                    message = "用户名或密码不能为空"
                }));
            }

            //pass = MD5Helper.MD5Encrypt32(pass);

            var user = await _sysUserInfoServices.Query(d => d.UserName == name && d.UserPWD == pass);

            if (user.Count > 0)
            {
                var userRoles = await _sysUserInfoServices.GetUserRoleNameStr(name, pass);

                //如果是基于用户的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.Name, name),
                    new Claim(JwtRegisteredClaimNames.Jti, user.FirstOrDefault().Id.ToString()),
                    new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString())
                };
                claims.AddRange(userRoles.Split(',').Select(s => new Claim(ClaimTypes.Role, s)));

                //用户标识
                var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
                identity.AddClaims(claims);

                var token = JwtToken.BuildJwtToken(claims.ToArray(), _requirement);
                return(new JsonResult(token));
            }
            else
            {
                return(new JsonResult(new
                {
                    success = false,
                    message = "认证失败"
                }));
            }
        }
Exemplo n.º 2
0
        public async Task <object> GetJwtStr(string name, string pass)
        {
            var userinfo = await _userInfoServices.Query(u => u.USER_LOGIN_NAME == name && u.USER_PASSWORD == pass);

            if (userinfo.Count == 1)
            {
                string jwtStr = string.Empty;
                bool   suc    = false;

                // 获取用户的角色名,请暂时忽略其内部是如何获取的,可以直接用 var userRole="Admin"; 来代替更好理解。
                var userRole = "Admin";//await _sysUserInfoServices.GetUserRoleNameStr(name, pass);
                if (userRole != null)
                {
                    // 将用户id和角色名,作为单独的自定义变量封装进 token 字符串中。
                    TokenModelJwt tokenModel = new TokenModelJwt {
                        Uid = 1, Role = userRole
                    };
                    jwtStr = JwtHelper.IssueJwt(tokenModel);//登录,获取到一定规则的 Token 令牌
                    suc    = true;
                }
                else
                {
                    jwtStr = "login fail!!!";
                }

                return(Ok(new
                {
                    success = suc,
                    token = jwtStr
                }));
            }
            else
            {
                return(BadRequest(new
                {
                    success = false,
                    errorcode = 400,
                    message = "当前用户不存在,请先注册用户!"
                }));
            }
        }