Exemple #1
0
        /// <summary>
        /// 保存登陆状态
        /// 为了和jwt保存同步 同用JwtRegisteredClaimNames.Jti
        /// </summary>
        /// <param name="jti"></param>
        /// <param name="user"></param>
        /// <param name="expires"></param>
        /// <param name="platform">0:web后台,1:app</param>
        /// <returns>当jwt标识登陆时返回string</returns>
        public string SignIn(string jti, Entities.Sys_User user, DateTime expires, int platform = 0)
        {
            string userDataJson = JsonConvert.SerializeObject(new UserData()
            {
                Id = user.Id, Name = user.Name, Account = user.Account, IsAdmin = user.IsAdmin
            });

            switch (platform)
            {
            case 0:
                List <Claim> claims = new List <Claim>()
                {
                    new Claim(JwtRegisteredClaimNames.Jti, jti),
                    new Claim(ClaimTypes.Sid, user.Id.ToString()),
                    new Claim(ClaimTypes.UserData, userDataJson)
                };
                ClaimsIdentity  claimsIdentity  = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
                ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
                _httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal, new AuthenticationProperties()
                {
                    ExpiresUtc = expires
                });
                break;

            case 1:
                return(_jwtFactory.CreateToken(new User()
                {
                    PrimarySid = platform, UserID = user.Id.ToString(), UserData = userDataJson, UserName = user.Name
                }, jti, expires));
            }
            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public (bool Status, string Message) AddUser(Entities.Sys_User user)
        {
            lock (addLock)
            {
                if (_dbContext.Sys_User.Any(o => o.Account == user.Account && !o.IsDeleted))
                {
                    return(Fail("用户账号已经存在"));
                }
                _dbContext.Sys_User.Add(user);
                _dbContext.SaveChanges();
                string newJson = JsonConvert.SerializeObject(user);
                _activityLogService.InsertedEntity <Entities.Sys_User>(user.Id, null, newJson, user.Creator);

                return(Success("添加成功"));
            }
        }