public HttpResponseMessage Post() { if (string.IsNullOrEmpty(LoginName) || string.IsNullOrEmpty(LoginPwd))//参数错误 { return(CreateResponseInfo(1001, string.Empty)); } UserInfo userInfo = _service.Login(LoginName, LoginPwd); if (userInfo == null)//密码错误 { return(CreateResponseInfo(1002, string.Empty)); } //产生令牌 string tokenValue = this.GetGuidString(); _service.CacheInfo(tokenValue + CacheString, tokenValue); //产生主站凭证 CreateInfo(tokenValue, userInfo); rsp = CreateResponseInfo(1000, tokenValue); HttpCookie tokenCookie = new HttpCookie("Token"); tokenCookie.Values.Add("Value", tokenValue); tokenCookie.Expires = DateTime.Now.AddMinutes(CacheTime); tokenCookie.Path = "/"; HttpContext.Current.Response.AppendCookie(tokenCookie); return(rsp); }
/// <summary> /// 用户登录 /// </summary> /// <param name="user"></param> /// <returns></returns> public ActionResult UserLogin(UserInfo user) { try { //object obj = Session["_vercode_"]; //if (obj == null) //return Json(new { Status = -9, Data = string.Empty, Msg = "验证码失效,\n请重新获取验证码" }); //string code = obj.ToString(); //if (string.IsNullOrEmpty(code)) //return Json(new { Status = -8, Data = string.Empty, Msg = "验证码无效,\n请重新获取验证码" }); if (user == null) { return(Json(new { Status = -7, Data = string.Empty, Msg = "参数错误,\n请输入用户信息" })); } //if(user.Vercode.Trim() != code.Trim()) //return Json(new { Status = -6, Data = string.Empty, Msg = "验证码错误,\n请重新输入验证码" }); if (string.IsNullOrEmpty(user.LoginName))//参数错误 { return(Json(new { Status = -5, Data = string.Empty, Msg = "参数错误" })); } UserInfo userInfo = _service.Login(user.LoginName, user.MD5LoginPwd); if (userInfo == null)//密码错误 { return(Json(new { Status = -4, Data = string.Empty, Msg = "密码错误" })); } //产生令牌 string tokenValue = this.GetGuidString(); _service.CacheInfo(tokenValue + CacheString, tokenValue); _service.CacheInfo(tokenValue, userInfo); HttpCookie tokenCookie = new HttpCookie("Token"); tokenCookie.Value = tokenValue; tokenCookie.Expires = DateTime.Now.AddMinutes(CacheTime); tokenCookie.Path = "/"; Response.AppendCookie(tokenCookie); return(Json(new { Status = 1, Data = tokenValue, Msg = string.Empty })); } catch (Exception ex) { return(Json(new { Status = -99, Data = string.Empty, Msg = ex.Message })); } }