public JsonResult Login(string code, string pwd) { //方便测试用,删除?? //if (string.IsNullOrEmpty(code)) //{ // code = "001"; // pwd = "a"; //} if (string.IsNullOrEmpty(code)) { return(Json(new { success = "error", message = "对不起,用户名不能为空" })); } if (string.IsNullOrEmpty(pwd)) { return(Json(new { success = "error", message = "对不起,密码不能为空" })); } // //测试测试 // DataTable dd = DataHelper.GetDataTable("select * from t_user"); //dd.TableName = "f**k"; //string aaa = new DtToXML().ConvertDataTableToXML(dd); Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("code", code); dic.Add("pwd", pwd); var model = new Dal.Basedata.t_userDal(dic, "code", "pwd").ToList(); if (model.Count == 0) { return(Json(new { success = "error", message = "对不起,用户名或者密码错误" })); } FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( 1, model.First().code, DateTime.Now, DateTime.Now.Add(FormsAuthentication.Timeout), false, model.First().name // JsonConvert.SerializeObject(model.First()) ); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); Response.Cookies.Add(cookie); HttpCookie cookienew = new HttpCookie("user"); cookienew.Value = model.First().code; Response.AppendCookie(cookienew); return(Json(new { success = "ok", message = "登陆成功,请稍等,正在跳转。。" })); }
public t_user getUser() { var context = HttpContext.Current; if (context == null) { throw new InvalidOperationException(); } var cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) { return(null); } var ticket = FormsAuthentication.Decrypt(cookie.Value); string code = ticket.Name; Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("code", code); var model = new Dal.Basedata.t_userDal(dic, "code").ToList(); if (model.Count == 0) { return(null); } return(model.First()); }