コード例 #1
0
ファイル: RepositoryBase.cs プロジェクト: SnailYan/Preoff
 /// <summary>
 /// 添加一条模型记录,自动提交更改
 /// </summary>
 public virtual bool Save(T entity)
 {
     try
     {
         int row   = 0;
         var entry = _dbContext.Entry <T>(entity);
         entry.State = EntityState.Added;
         row         = _dbContext.SaveChanges();
         entry.State = EntityState.Detached;
         return(row > 0);
     }
     catch (Exception e)
     {
         throw e;
     }
 }
コード例 #2
0
        public bool UpdateStatus(int execTaskId, int statusId, DateTime endTime)
        {
            ExecTaskTable _etable = _dbcontext.ExecTaskTable.FirstOrDefault(p => p.Id == execTaskId);

            if (_etable != null)
            {
                _etable.TaskStateTableId = statusId;
                if (endTime != null && endTime.Year != 1)
                {
                    _etable.EndTime = endTime;
                }
                var entity = _dbcontext.Entry(_etable);
                entity.State = EntityState.Modified;
                int row = _dbcontext.SaveChanges();
                entity.State = EntityState.Detached;
                return(row == 1);
            }
            else
            {
                return(false);
            }
        }
コード例 #3
0
        public IActionResult Token([FromBody] AuthorizeTable _auth)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var a = _dbContext.UserTable.FirstOrDefault(u => (u.LoginName == _auth.userName) && (u.LoginPwd == Pwd.Ecoding(_auth.password)));
                    if (a is null)
                    {
                        return(Json(new
                        {
                            state = "-1",
                            msg = "账号不存在或密码错误!"
                        }));
                    }
                    a.LoginCount    = (a.LoginCount is null) ? a.LoginCount = 1 : a.LoginCount + 1;
                    a.LastLoginTime = DateTime.Now;
                    //a.LoginCount = a.LoginCount + 1;
                    _dbContext.UserTable.Update(a);
                    _dbContext.SaveChanges();
                    //var claims=new Claim[]{
                    //    new Claim(ClaimTypes.Name,userModel.CName),
                    //    new Claim(ClaimTypes.Role,"user"),
                    //    //new Claim("SuperAdminOnly","true")
                    //};

                    var claims = new Claim[] {
                        new Claim(ClaimTypes.Name, _auth.userName),
                        new Claim(ClaimTypes.Role, "user"),
                        //new Claim("SuperAdminOnly","true")
                    };


                    var key   = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey));
                    var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

                    var token = new JwtSecurityToken(
                        _jwtSettings.Issuer,
                        _jwtSettings.Audience,
                        claims,
                        DateTime.Now, DateTime.Now.AddMinutes(_jwtSettings.TimeOut),
                        creds);
                    TokenUser _tokenUser = new TokenUser
                    {
                        token = new JwtSecurityTokenHandler().WriteToken(token),
                        user  = a,
                        state = "0",
                        msg   = "操作成功!"
                    };
                    //return Ok(new {token=new JwtSecurityTokenHandler().WriteToken(token)});
                    return(Ok(_tokenUser));
                }
                return(Json(new
                {
                    state = "-1",
                    msg = "非法操作!"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    state = "-1",
                    msg = "非法操作!"
                }));
            }
        }