/// <summary> /// 验证用户 /// </summary> /// <param name="account"></param> /// <param name="password"></param> /// <returns></returns> public bool Validate(string account, string password) { if (string.IsNullOrWhiteSpace(account) || string.IsNullOrWhiteSpace(password)) { return(false); } password = StringFactory.HashBySolt(password); return(UserSecurityInfoRepo .Read() .Exists(us => us.Account.ToLower() == account.ToLower() && us.Password == password)); }
public void SetPassword(string account, string password) { if (string.IsNullOrWhiteSpace(account) || string.IsNullOrWhiteSpace(password)) { throw new Exception("设置密码时发生参数错误"); } UserSecurityInfo securityInfo = UserSecurityInfoRepo .Read() .FirstOrDefault(us => us.Account.ToLower() == account.ToLower()); if (securityInfo == null) { securityInfo = new UserSecurityInfo(); securityInfo.Id = StringFactory.NewGuid(); } securityInfo.Account = account; securityInfo.Password = StringFactory.HashBySolt(password); UserSecurityInfoRepo.Save(securityInfo); }
/// <summary> /// 登陆 /// </summary> /// <param name="parameter"></param> /// <returns></returns> public static UserState CreateState(AuthParameter parameter) { #region 检查参数 if (parameter == null || string.IsNullOrWhiteSpace(parameter.Type) || string.IsNullOrWhiteSpace(parameter.Account)) { return(null); } #endregion #region 验证身份 if (parameter.Type == AuthType.PASSWORD && !string.IsNullOrWhiteSpace(parameter.Password)) { //通过密码验证 bool validateResult = passport.Validate(parameter.Account, parameter.Password); if (!validateResult) { return(null); } } else if (parameter.Type == AuthType.CLIENT && !string.IsNullOrWhiteSpace(parameter.AppKey) && !string.IsNullOrWhiteSpace(parameter.AppIp)) { //通过信任的 App 验证 App app = App.GetApp(parameter.AppKey); if (app == null || app.Secret != parameter.AppSecret) {//如果 app 不存在或 secret 错误,则返回 null ,验证失败 return(null); } else { app.WhiteList = app.WhiteList ?? ""; var whiteList = app.WhiteList.Split(',') .Select(ip => (ip ?? "").Trim()) .ToList(); whiteList.AddRange(new List <string> { "localhost", "127.0.0.1", "::1" }); var appIp = (parameter.AppIp ?? "").Trim(); if (!whiteList.Exists(ip => !string.IsNullOrWhiteSpace(ip) && !string.IsNullOrWhiteSpace(appIp) && ip == appIp)) { return(null); } } } else { return(null); } #endregion #region 检查用户状态 var existUser = User.GetUser(parameter.Account); if (existUser == null || !existUser.IsActive) { return(null); } #endregion parameter.Device = parameter.Device ?? ""; parameter.DeviceId = parameter.DeviceId ?? ""; parameter.Ip = parameter.Ip ?? ""; //检查在同一 DeviceId 下是否已存在状态 UserState existUserState = UserState.GetState(parameter.Account, parameter.DeviceId); if (existUserState != null) { //利用当前类中 GetState 方法中写好的更新最后活动时间 return(GetState(existUserState.Token)); } //创建新用户会话状态开始 //生成Token string srcToken = StringFactory.HashBySolt(StringFactory.NewGuid()); UserState newUserState = new UserState(); newUserState.Account = parameter.Account; newUserState.LastActive = DateTime.Now; //--存储MD5后的Token,有数据库权限的人也不应该有查看别人会话密钥的权力 newUserState.Token = srcToken; //Encrypt.EncodeBySolt(srcToken); newUserState.Device = parameter.Device; newUserState.DeviceId = parameter.DeviceId ?? parameter.Ip; newUserState.Ip = parameter.Ip; newUserState.Save(); //创建用户会话状态结束 //向上层提供原Token newUserState.Token = srcToken; // return(newUserState); }