public bool UserAuthentication(string userId, string userPwd, string hostIP, ref string message, ref string encryptTicket) { try { message = ""; string sql = "from YnUser where userId='" + userId + "' or userName ='******'"; IList <YnUser> ilist = YnDaoHelper.GetInstance().nHibernateHelper.Find <YnUser>(sql); if (ilist == null || ilist.Count() == 0) { return(false); } YnFrame.Dal.Entities.YnUser ynUser = ilist[0]; if (YnFrame.Services.YnUserService.GetInstance().ValidateUser(ynUser.userId, userPwd)) { //YnFrame.Dal.Entities.YnUser ynUser = YnFrame.Services.YnUserService.GetInstance().Get(userId); string sUserData = Newtonsoft.Json.JsonConvert.SerializeObject(ynUser.GetTicket()); bool createPersistentCookie = false; DateTime dt = createPersistentCookie ? DateTime.Now.AddMinutes(99999) : DateTime.Now.AddDays(365); //用一年看他还过不过期 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, // 票据版本号 userId.ToString(), // 票据持有者 DateTime.Now, //分配票据的时间 dt, // 失效时间 createPersistentCookie, // 需要用户的 cookie sUserData, // 用户数据,这里其实就是用户的角色 FormsAuthentication.FormsCookiePath); //cookie有效路径 //System.Web.Security.FormsAuthenticationTicket ticket = YnFrame.Web.FormsAuthenticationService.GetInstance().GetTicket(userId, 0, ynUser, false); string hash = System.Web.Security.FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, hash); //加密之后的cookie if (ticket.IsPersistent) { cookie.Expires = ticket.Expiration; } encryptTicket = hash; //添加cookie到页面请求响应中 HttpContext.Current.Response.Cookies.Add(cookie); //写日志 ynUser.lastLoginIp = hostIP; ynUser.lastLoginDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); YnUserService.GetInstance().Update(ynUser); return(true); } } catch (Exception ex) { message = ex.Message; } return(false); }
public YnWebRight GetYnWebRight() { YnWebRight ynWebRight = new YnWebRight(); //string mi = System.Web.HttpContext.Request["mi"]; //System.Web.HttpContext.Current.Server.UrlEncode(ynWebAccMenuTree.ynWebModule.parameter); string mi = System.Web.HttpContext.Current.Request["mi"]; YnFrame.Dal.Entities.YnUser ynUser = FormsAuthenticationService.GetInstance().GetTicketUserData(); //ynUser = YnFrame.Services.YnUserService.GetInstance().Get(ynUser.userId); if (ynUser != null && mi != null) { ynWebRight = GetYnWebRight(ynUser.userId, mi); } return(ynWebRight); }
public string GetUserBaseInfo(string userId, ref string message) { try { message = ""; YnFrame.Dal.Entities.YnUser ynUser = YnFrame.Services.YnUserService.GetInstance().Get(userId); return(YnBaseClass2.Helper.ObjectHelper.Serialize(ynUser.GetTicket())); } catch (Exception ex) { message = ex.Message; } return(null); }
public string GetUser(string ticket, string userId, ref string message) { try { message = ""; if (!IsTicketValid(ticket)) { throw new Exception("票证验证失败!"); } YnFrame.Dal.Entities.YnUser ynUser = YnFrame.Services.YnUserService.GetInstance().Get(userId); return(YnBaseClass2.Helper.ObjectHelper.Serialize(ynUser)); } catch (Exception ex) { message = ex.Message; } return(null); }