Exemple #1
0
        public JsonResult Login(string name, string pwd, string role = "")
        {
            ResponseResult result = new ResponseResult();

            if (name == null || pwd == null)
            {
                return(Json(result));
            }
            User user;

            if (!SqliteHelper.Instacne.CheckUser(name, pwd, out user))
            {
                result.code = -2;
                result.msg  = "账号或密码错误";
                return(Json(result));
            }
            //如果是基于角色的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色
            var claims = new Claim[] { new Claim(ClaimTypes.Name, name), new Claim(ClaimTypes.Role, role) };
            //用户标识
            var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);

            identity.AddClaims(claims);
            var authorization = JwtToken.BuildJwtTokenString(claims, _requirement);

            user.authorization = authorization;
            result.SetResult(user);
            return(Json(result));
        }