public Response UserLogin([FromBody] LoginInPutModel loginInputModel) { var response = new Response { HttpStatusCode = HttpStatusCode.Continue }; try { var result = _iLoginApplication.UserLogin(loginInputModel); if (result != null) { _iCacheContext.Set("LoginToken", result.UserName, DateTime.Now.AddDays(10)); response.HttpStatusCode = HttpStatusCode.OK; } else { if (!_iLoginApplication.IsUserNameExists(loginInputModel)) { response.Message = "UserNameNotExists"; } else if (!_iLoginApplication.IsUserPasswordExists(loginInputModel)) { response.Message = "UserPasswordNotExists"; } } return(response); } catch (Exception ex) { response.HttpStatusCode = HttpStatusCode.InternalServerError; Console.WriteLine(ex); } return(response); }
public LoginResult Do(PassportLoginRequest model) { var result = new LoginResult(); try { model.Trim(); //获取用户信息 User userInfo = null; //if (model.Account == "System") //{ // userInfo = new User // { // Id = Guid.Empty.ToString(), // Account = "System", // Name = "超级管理员", // Password = "******" // }; //} //else //{ // userInfo = _app.FindSingle(u => u.Account == model.Account); //} userInfo = _app.FindSingle(u => u.Account == model.Account); if (userInfo == null) { throw new Exception("用户不存在"); } if (userInfo.Password != StringExtensions.ToMd5(model.Password)) { throw new Exception("密码错误"); } var currentSession = new UserAuthSession { Account = model.Account, Name = userInfo.Name, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), AppKey = model.AppKey, CreateTime = DateTime.Now // , IpAddress = HttpContext.Current.Request.UserHostAddress }; //创建Session _cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Code = 200; result.ReturnUrl = ""; result.Token = currentSession.Token; } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return(result); }
public LoginResult Do(PassportLoginRequest model) { var result = new LoginResult(); try { model.Trim(); //获取应用信息 var appInfo = _unitWork.FindSingle <SysInfo>(u => u.AppKey == model.AppKey); if (appInfo == null) { throw new Exception("应用不存在"); } else { if (Encryption.Decrypt(appInfo.AppSecret) != "hhweb2.0") { throw new Exception("应用密钥不正确!"); } } //获取用户信息 var userInfo = _unitWork.FindSingle <SysUser>(u => u.Account == model.Account); if (userInfo == null) { throw new Exception("用户不存在"); } if (Encryption.Decrypt(userInfo.Password) != model.Password) { throw new Exception("密码错误"); } var currentSession = new UserAuthSession { Account = model.Account, Name = userInfo.Name, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), AppKey = model.AppKey, CreateTime = DateTime.Now, }; //创建Session _cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Code = 200; result.ReturnUrl = appInfo.ReturnUrl; result.Token = currentSession.Token; } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return(result); }
public LoginResult Do(PassportLoginRequest model) { var result = new LoginResult(); try { model.Trim(); //获取应用信息 var appInfo = _appInfoService.Get(model.AppKey); if (appInfo == null) { throw new Exception("应用不存在"); } //获取用户信息 var userInfo = _app.FindSingle(u => u.Account == model.Account); if (userInfo == null) { throw new Exception("用户不存在"); } if (userInfo.Password != Md5.GetMD5String(model.Password)) { throw new Exception("密码错误"); } var currentSession = new UserAuthSession { Account = model.Account, Name = userInfo.Name, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), AppKey = model.AppKey, CreateTime = DateTime.Now // , IpAddress = HttpContext.Current.Request.UserHostAddress }; //创建Session _cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Code = 200; result.ReturnUrl = appInfo.ReturnUrl; result.Token = currentSession.Token; } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return(result); }
/// <summary> /// 用户名密码登录 /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> /// <exception cref="Exception"></exception> public Response Login(string username, string password) { var result = new Response <string>(); try { // 密码加密 password = Md5.Encrypt(password); // 防sql注入 username = Md5.avoidSqlInjection(username); var userInfo = SimpleDb.GetSingle(u => u.userName.Equals(username)); if (userInfo == null) { throw new Exception("用户不存在"); } if (!Md5.Encrypt(userInfo.passWord).Equals(password)) { throw new Exception("密码不正确"); } var currentSession = new UserAuthSession { UserId = userInfo.userId, WechatUserId = userInfo.wechatUserId, UserName = userInfo.userName, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), CreateTime = DateTime.Now }; _cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Result = currentSession.Token; } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return(result); }
public IActionResult GetToken(string username, string password) { string accessToken = _cache.Get <string>(username); if (!(accessToken?.Length > 0)) { UserModel user = UserMock.FindUser(username, password); Dictionary <string, object> keyValuePairs = new Dictionary <string, object>(); keyValuePairs.Add(nameof(user.Id), user.Id); keyValuePairs.Add(nameof(user.UserName), user.UserName); keyValuePairs.Add(nameof(user.Phone), user.Phone); keyValuePairs.Add(nameof(user.Email), user.Email); accessToken = _tokenContext.GetToken(keyValuePairs, 120); _cache.Set(user.UserName, accessToken, DateTime.Now.AddHours(2)); } return(Json(new { access_token = accessToken })); }
public LoginResult Do(PassportLoginRequest model) { var result = new LoginResult(); try { model.Trim(); //获取应用信息 var appInfo = _appInfoService.Get(model.AppKey); if (appInfo == null) { throw new Exception("应用不存在"); } //获取用户信息 User userInfo = null; if (model.Account == Define.SYSTEM_USERNAME) { userInfo = new User { Id = Guid.Empty.ToString(), Account = Define.SYSTEM_USERNAME, Name = "超级管理员", Password = Define.SYSTEM_USERPWD }; } else { userInfo = _app.FirstOrDefault(u => u.Account == model.Account); } if (userInfo == null) { throw new Exception("用户不存在"); } if (userInfo.Password != model.Password) { throw new Exception("密码错误"); } if (userInfo.Status != 0) { throw new Exception("账号状态异常,可能已停用"); } var currentSession = new UserAuthSession { Account = model.Account, Name = userInfo.Name, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), AppKey = model.AppKey, CreateTime = DateTime.Now // , IpAddress = HttpContext.Current.Request.UserHostAddress }; //创建Session _cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Code = 200; result.ReturnUrl = appInfo.ReturnUrl; result.Token = currentSession.Token; } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return(result); }
public LoginResult Do(PassportLoginRequest model) { var result = new LoginResult { Code = 500 }; try { //判断是否已经登录 var userAuthSession = _cacheContext.Get <UserAuthSession>(model.Account); if (userAuthSession != null) { //设置报错消息 SetError(model.Account, ref result, "txtUserID", "Login", "E001", "该用户名已经登陆,不能重复登陆。"); return(result); } //Stopwatch watch = new Stopwatch(); //watch.Start(); //var testInfo = _userInfoApp.FindSingle(u => u.Id == model.Account); //watch.Stop(); //result.Code = 200; //result.Message = string.Format("用时{0}毫秒", watch.ElapsedMilliseconds); //return result; var data = _loginApp.GetUserInfoByUserId(model.Account); //用户名 不存在 if (data == null || data.data == null) { //设置报错消息 SetError(model.Account, ref result, "txtUserID", "Login", "E002", "用户名或密码错误"); return(result); } //获取匿名类型的属性值 var pdc = TypeDescriptor.GetProperties(data.data); var pdId = pdc.Find("Password", true); string passwordDataBase = pdId.GetValue(data.data); //对输入密码进行加密 string decryptPassword = StringUtil.EncryptPasswordWitdhMD5(model.Password); //两密码不同时报错 if (!decryptPassword.Equals(passwordDataBase)) { //设置报错消息 string[] param = { "密码" }; SetError(model.Account, ref result, "txtPassword", "Common", "E003", "用户名或密码错误"); return(result); } //UserOpenDate UserCloseDate pdId = pdc.Find("UserOpenDate", true); DateTime userOpenDate = pdId.GetValue(data.data); pdId = pdc.Find("UserCloseDate", true); DateTime userCloseDate = pdId.GetValue(data.data); if (DateTime.Now < userOpenDate) { SetError(model.Account, ref result, "txtPassword", "Common", "E003", "账户未生效"); return(result); } if (DateTime.Now > userCloseDate) { SetError(model.Account, ref result, "txtPassword", "Common", "E003", "账户已过期"); return(result); } //判断是否被锁定 pdId = pdc.Find("LockFlag", true); string lockFlag = pdId.GetValue(data.data); if (HAD_LOCKED.Equals(lockFlag)) { //设置报错消息 SetError(model.Account, ref result, "txtUserID", "Login", "E003", "账户被锁定"); return(result); } //获得当前时间 string toDay = DateTime.Now.ToString("yyyyMMdd"); pdId = pdc.Find("CompanyOpenDate", true); //获得用户公司服务开始时间 string startDate = (string)pdId.GetValue(data.data); //获得用户公司服务结束时间 pdId = pdc.Find("CompanyCloseDate", true); string endDate = (string)pdId.GetValue(data.data); //当前时间不在公司服务时间范围内时报错 if (toDay.CompareTo(endDate) > 0 || toDay.CompareTo(startDate) < 0) { //设置报错消息 string[] param = { "公司" }; SetError(model.Account, ref result, "txtUserID", "Login", "E004", "公司不在服务时间"); return(result); } //获得用户ID服务开始时间 startDate = userOpenDate.ToString("yyyyMMdd"); //获得用户ID服务结束时间 endDate = userCloseDate.ToString("yyyyMMdd"); //当前时间不在用户ID服务时间范围内时报错 if (toDay.CompareTo(endDate) > 0 || toDay.CompareTo(startDate) < 0) { //设置报错消息 string[] param = { "用户名" }; SetError(model.Account, ref result, "txtUserID", "Login", "E004", "用户不在服务时间"); return(result); } //获得用户公司代码 pdId = pdc.Find("CompanyCD", true); var companyCD = (string)pdId.GetValue(data.data); //获得用户姓名 pdId = pdc.Find("UserName", true); var userName = (string)pdId.GetValue(data.data); //获取是否超管信息 pdId = pdc.Find("IsRoot", true); var IsRoot = (string)pdId.GetValue(data.data); var role = GetRoleInfo(model.Account, companyCD); //只读取顶级菜单信息 //全部菜单信息放到LEFT.CS内读取并缓存 //获得用户菜单信息 var menuInfo = _commonApp.GetMenuData(model.Account, companyCD, true); //获得用户页面操作信息 DataTable authoInfo = null;//SafeUtil.InitPageAuthority(userID, companyCD); //设置Session中用户信息 var userInfo = new UserInfoUtil(); var companyInfo = _companyApp.FindSingle(u => u.Id == companyCD); if (companyInfo != null) { userInfo.CompanyName = companyInfo.NameCn; } //设置用户ID userInfo.UserID = model.Account; //设置用户姓名 userInfo.UserName = userName; //设置超管信息 userInfo.IsRoot = IsRoot; //设置用户公司代码 userInfo.CompanyCD = companyCD; pdId = pdc.Find("EmployeeID", true); //获取人员编号 string employeeID = (string)pdId.GetValue(data.data); //人员ID设置的场合,设置人员ID if (!string.IsNullOrEmpty(employeeID)) { //设置人员ID userInfo.EmployeeID = employeeID; //设置人员名 pdId = pdc.Find("EmployeeName", true); userInfo.EmployeeName = (string)pdId.GetValue(data.data); //设置人员工号 pdId = pdc.Find("EmployeeNum", true); userInfo.EmployeeNum = (string)pdId.GetValue(data.data); //获取部门ID pdId = pdc.Find("DeptID", true); string deptID = (string)pdId.GetValue(data.data); //部门ID设置的场合,设置部门ID if (!string.IsNullOrEmpty(deptID)) { //设置部门ID userInfo.DeptID = deptID; } //部门名称 pdId = pdc.Find("DeptName", true); userInfo.DeptName = (string)pdId.GetValue(data.data); } //设置角色列表 userInfo.Role = role; //设置用户菜单信息 userInfo.MenuInfo = menuInfo; //设置用户页面操作信息 userInfo.AuthorityInfo = authoInfo; //出入库价格是否显示 userInfo.IsDisplayPrice = _parameterSettingApp.Get(userInfo.CompanyCD, "1", true); //是否启用条码 userInfo.IsBarCode = _parameterSettingApp.Get(userInfo.CompanyCD, "2", true); //是否启用多计量单位 userInfo.IsMoreUnit = _parameterSettingApp.Get(userInfo.CompanyCD, "3", false); //是否启用自动生成凭证 userInfo.IsVoucher = _parameterSettingApp.Get(userInfo.CompanyCD, "6", false); //是否启用自动审核登帐 userInfo.IsApply = _parameterSettingApp.Get(userInfo.CompanyCD, "7", false); //是否启用超订单发货 userInfo.IsOverOrder = _parameterSettingApp.Get(userInfo.CompanyCD, "8", false); //允许出入库价格为零 userInfo.IsZero = _parameterSettingApp.Get(userInfo.CompanyCD, "9", false); //小数位数 userInfo.SelPoint = "2";//默认 var dtPoint = _parameterSettingApp.GetPoint(userInfo.CompanyCD, "5"); if (dtPoint?.Rows.Count > 0) { userInfo.SelPoint = dtPoint.Rows[0]["SelPoint"].ToString(); } //var getUserInfo = _userInfoApp.FindSingle(u => u.CompanyCD == companyCD&&u.Id== model.Account); //getUserInfo.LastLoginTime=DateTime.Now; _userInfoApp.Update(u => u.CompanyCD == companyCD && u.Id == model.Account, u => new UserInfo { LastLoginTime = DateTime.Now }); var currentSession = new UserAuthSession { Account = model.Account, Name = userInfo.UserName, Token = Guid.NewGuid().ToString().GetHashCode().ToString("x"), AppKey = model.AppKey, CreateTime = DateTime.Now, UserInfo = userInfo // , IpAddress = HttpContext.Current.Request.UserHostAddress }; //创建Session _cacheContext.Set(currentSession.Token, currentSession, DateTime.Now.AddDays(10)); result.Code = 200; result.Token = currentSession.Token; } catch (Exception ex) { result.Code = 500; result.Message = ex.Message; } return(result); }