public object login(LoginInput input) { var user = (from u in this.db.users where u.username == input.username select u).FirstOrDefault(); if (user != null) { if (user.password == input.password) { var token = new JwtBuilder() .WithAlgorithm(new HMACSHA256Algorithm()) .WithSecret(Config.secret) .AddClaim("user", user) .Build(); user.token = token; return(CommonRtn.Success("user", user)); } else { return(CommonRtn.Error("密码错误")); } } else { return(CommonRtn.Error("用户不存在")); } }
public Rtn <User> login([FromBody] LoginInput input) { var user = (from u in this.db.users where u.username == input.username select new User { id = u.id, roleList = (from r in this.db.roles where ("," + u.roleIds + ",").Contains("," + r.id + ",") select r).ToList(), password = u.password }).FirstOrDefault(); if (user != null) { if (user.password == input.password) { var token = new JwtBuilder() .WithAlgorithm(new HMACSHA256Algorithm()) .WithSecret(Config.secret) .AddClaim("user", new User { id = user.id }) .Build(); var menuIds = new List <string> (); foreach (var r in user.roleList) { foreach (var mid in r.menuIds.Split(",")) { menuIds.Add(mid); } } Console.WriteLine(menuIds.ToArray().Length); var ids = menuIds.Distinct(); user.menuList = (from m in this.db.menus where ids.Contains(m.id.ToString()) select m).ToList(); return(Rtn <User> .Success(user)); } else { return(Rtn <User> .Error("密码错误")); } } else { return(Rtn <User> .Error("用户不存在")); } }
public CommonRtn signup([FromBody] LoginInput input) { var user = (from u in this.db.users where u.username == input.username select u).FirstOrDefault(); if (user != null) { return(CommonRtn.Error("用户已经注册")); } else { var newUser = new User { nickname = "新用户", username = input.username, password = input.password, roleType = RoleType.User }; this.db.users.Add(newUser); this.db.SaveChanges(); return(CommonRtn.Success(new Dictionary <string, object> { { "user", newUser } })); } }