コード例 #1
0
        public OkObjectResult Post(string username, string password, string name, string logo, string desc, string email, string phone)
        {
            VsSysUser pExist = _userService.QueryByID(username);

            if (pExist != null)
            {
                return(JsonRes.Fail("用户名已存在"));
            }
            // 循环将所有接口写入权限表
            List <VsSysApiModule> pFindModule = _apiModuleService.Query();

            foreach (var module in pFindModule)
            {
                VsSysPower powerEntity = new VsSysPower
                {
                    Id           = System.Guid.NewGuid().ToString(),
                    SUserid      = username,
                    SModuleid    = module.SModuleid,
                    SApimoduleid = module.Id,
                    BIsdeleted   = false,
                };
                string powerError = "";
                int    powerRes   = _powerService.TryAdd(out powerError, powerEntity);
                if (powerRes == 0)
                {
                    return(JsonRes.Fail(powerEntity, powerError));
                }
            }
            VsSysUser entity = new VsSysUser
            {
                SUsername   = username,
                SPassword   = MD5Generate.Encrypt(password),
                SName       = name,
                DCreatetime = DateTime.Now.ToLocalTime(),
                SLogo       = logo,
                SDesc       = desc,
                SEmail      = email,
                SPhone      = phone
            };
            string error = "";
            int    res   = _userService.TryAdd(out error, entity);

            if (res == 0)
            {
                return(JsonRes.Fail(entity, error));
            }
            return(JsonRes.Success(entity));
        }
コード例 #2
0
        public object GetJWTToken3(string username, string password)
        {
            string jwtStr = string.Empty;
            bool   suc    = false;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return(JsonRes.Fail("用户名或密码不能为空"));
            }

            var enPassword = MD5Generate.Encrypt(password);
            var user       = _userService.QuerySingle(d => d.SUsername == username);

            if (user != null)
            {
                //更新用户最近登录时间
                user.DLastlogin = DateTime.Now.ToLocalTime();
                string error = "";
                int    res   = _userService.TryUpdate(out error, user);
                if (res == 0)
                {
                    return(JsonRes.Fail(user, error));
                }
                //如果是基于用户的授权策略,这里要添加用户;如果是基于角色的授权策略,这里要添加角色
                var claims = new List <Claim> {
                    new Claim(ClaimTypes.UserData, username), // 使用用户id认证授权
                    new Claim(JwtRegisteredClaimNames.Jti, username),
                    new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_requirement.Expiration.TotalSeconds).ToString())
                };

                var token = JwtToken.BuildJwtToken(claims.ToArray(), _requirement);
                return(JsonRes.Success(token));
            }
            else
            {
                return(JsonRes.Fail("认证失败"));
            }
        }