/// <summary> /// 修改密码 /// </summary> /// <param name="old_pwd"></param> /// <param name="NewPassword"></param> /// <returns></returns> public JsonResult SetPassword(string user_code, string old_pwd, string new_pwd) { var result = false; //验证 old_pwd = old_pwd.Trim(); bool success = UserValidatorFactory.Login(OThinker.Clusterware.AuthenticationType.Forms, null, user_code, old_pwd, OThinker.H3.Site.PortalType.Portal); if (success) { //this.UserValidator.User.Password = new_pwd; var u = Engine.Organization.GetUserByCode(user_code); u.Password = new_pwd; //var user_updated = Engine.Organization.GetUnit(u.ObjectID); Engine.Organization.UpdateUnit(user_code, u); result = true; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public JsonResult SecureLogin(string userCode, string password) { bool loginResult = false; object result; string enableCheckCode = System.Configuration.ConfigurationManager.AppSettings["EnableCheckCode"] + string.Empty; try { if (enableCheckCode != "0") { string[] wait = System.Configuration.ConfigurationManager.AppSettings["WaitTime"].Split(','); var sql = "SELECT COUNT(1) FROM OT_CHECKCODE WHERE STATE = 0 AND USERCODE = N'" + userCode.Replace("'", "''") + "'"; int c = Convert.ToInt32(Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteScalar(sql) + string.Empty); int waits = 0; int.TryParse(wait[c >= wait.Length ? wait.Length - 1 : c], out waits); string ret = Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteScalar("SELECT Max(CREATETIME) FROM OT_CHECKCODE WHERE STATE = 0 AND USERCODE = N'" + userCode.Replace("'", "''") + "'") + string.Empty; DateTime dt = Convert.ToDateTime(string.IsNullOrWhiteSpace(ret) ? "2019-01-01 00:00:00" : ret); if (waits > 0 && (DateTime.Now - dt).TotalSeconds < waits) { return(Json(new { Success = false, Message = "NeedCheckCode" }, JsonRequestBehavior.AllowGet)); } } loginResult = UserValidatorFactory.Login( OThinker.Clusterware.AuthenticationType.Forms, string.Empty, userCode, password, OThinker.H3.Site.PortalType.Portal); } catch (Exception ex) { //ConnectionFailed if (ex.Message.Contains("ConnectionFailed")) { return(Json(new { Success = false, Message = "ConnectionFailed" }, JsonRequestBehavior.AllowGet)); } else if (ex.Message.Contains("PasswordInvalid")) { return(Json(new { Success = false, Message = "EnginePasswordInvalid" }, JsonRequestBehavior.AllowGet)); } } if (loginResult) { if (enableCheckCode != "0") { string sql = "UPDATE OT_CHECKCODE SET STATE = 1 WHERE USERCODE = N'" + userCode.Replace("'", "''") + "'"; Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteNonQuery(sql); } #region 校验密码复杂度 //Regex reg = new Regex(@"^(((?=.*[0-9])(?=.*[a-zA-Z]))|((?=.*[0-9])(?=.*[!@#$%\^&*\(\)]))|((?=.*[a-zA-Z])(?=.*[!@#$%\^&*\(\)]))).{6,16}$", RegexOptions.None); if (!RegValidate(password)) { UserValidatorFactory.Exit(this); Session.Clear(); Session.Abandon(); result = new { Success = false, ErrorCode = 1, Message = "密码复杂度不符合要求" }; } else { result = getCurrentUser(); FormsAuthentication.SetAuthCookie(this.UserValidator.User.Code, false); } #endregion } else { result = new { Success = false, ErrorCode = 2, Message = "用户名或密码错误" }; if (enableCheckCode != "0") { string model = "INSERT INTO OT_CHECKCODE(OBJECTID,USERCODE,IP,SYSTEMINFO,BROWSER,CODE,STATE,LOGINTIME, CREATETIME)VALUES('[OBJECTID]','" + userCode.Replace("'", "''") + "','" + Request.UserHostAddress.Replace("'", "''") + "','" + Request.UserAgent.Replace("'", "''") + "','" + Request.Browser.Browser.Replace("'", "''") + "/" + Request.Browser.Version.Replace("'", "''") + "','',[STATE],to_date('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-MM-dd HH24:mi:ss'),to_date('" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','yyyy-MM-dd HH24:mi:ss'))"; string sql = model.Replace("[OBJECTID]", Guid.NewGuid().ToString()).Replace("[STATE]", "0"); Engine.EngineConfig.CommandFactory.CreateCommand().ExecuteNonQuery(sql); } UserValidatorFactory.Exit(this); Session.Clear(); Session.Abandon(); } return(Json(result, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 登录事件 /// </summary> /// <param name="usercode"></param> /// <param name="password"></param> /// <param name="systemcode"></param> /// <returns></returns> public string DoLogin(string usercode, string password, string systemcode) { var context = HttpContext.Current; //使用H3的认证方式,支持Form认证和AD认证 var loginResult = UserValidatorFactory.Login( OThinker.Clusterware.AuthenticationType.Forms, string.Empty, usercode, password, OThinker.H3.Site.PortalType.Portal); if (loginResult) { var user = OThinker.H3.Controllers.AppUtility.Engine.Organization.GetUserByCode(usercode); //此处客户要求每个系统单独生成Token,做的处理,只要任一系统登录成功,都为接入单点登录的所有网站生成Token //查找注册在H3单点登录列表中的所有站点 var systemlist = OThinker.H3.Controllers.AppUtility.Engine.SSOManager.GetSSOSystemList(); foreach (var item in systemlist) { //5 登录成功,创建用户账号对应的token xxx //Token的加密组合:系统编码+登录名+时间戳 var p_param = string.Format("{0}|{1}|{2}", item.SystemCode, usercode, System.DateTime.Now.Ticks); var key = GetSecretBySystemcode(item.SystemCode); var token = EncryptHelper.Encrypt(p_param, key); //更改系统状态,允许调用接口进行Token验证 item.AllowGetToken = true; OThinker.H3.Controllers.AppUtility.Engine.SSOManager.UpdateSSOSystem(item); //6 把token写到本站cookie; context.Response.SetCookie(new HttpCookie(item.SystemCode, token)); } //这个cookie和sso流程无关,是方便SSO的login.html前端页面显示用户名用的。 context.Response.SetCookie(new HttpCookie("username", user.Name)); var mesg = new Message { UserCode = user.Code }; if (!string.IsNullOrEmpty(systemcode)) { //7 跳转到returnurl并带上token。此处只输出token,在前端页面回调中执行跳转。 mesg.Url = GetSystemUrl(systemcode); mesg.Token = context.Request.Cookies[systemcode] != null ? context.Request.Cookies[systemcode].Value : ""; } return(Newtonsoft.Json.JsonConvert.SerializeObject(mesg)); } else { var mesg = new Message { UserCode = "", ErrCode = "1000", ErrMsg = "用户名或密码错误" }; return(Newtonsoft.Json.JsonConvert.SerializeObject(mesg)); } }