public ActionResult Login(string mobile, string password) { ViewBag.Title = "登录"; if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(password)) { ViewBag.Error = "手机号或密码为空"; return(View()); } var user = _dal.GetByMobile(mobile); if (user == null || user.Password != password) { ViewBag.Error = "手机号或密码错误"; return(View()); } if (string.IsNullOrEmpty(user.WeChatId)) { OAuthHelper.DoOAuth(HttpContext, mobile); return(new EmptyResult()); } var userInfo = new CurrentVipModel { VipId = user.Id, OpenId = user.WeChatId, pwd = EncryptHelper.Md5(password) }; SetAuthCookie(userInfo); HttpContext.Items["CurrentVip"] = userInfo; return(RedirectToAction("Index", "User")); }
public ActionResult Callback(string code, string state) { if (string.IsNullOrEmpty(code)) { return(Content("授权无效")); } //获取token try { var tokenResult = OAuthApi.GetAccessToken(AppConfig.Instance.AppId, AppConfig.Instance.AppSecret, code); if (tokenResult.errcode != Senparc.Weixin.ReturnCode.请求成功) { return(Content(tokenResult.errcode + ":" + tokenResult.errmsg)); } var openId = tokenResult.openid; if (Session["VipMobile"] != null) { var vip = _dal.GetByMobile(Session["VipMobile"].ToString()); if (vip != null) { vip.WeChatId = openId; vip.UpdatedTime = DateTime.Now; _dal.Update(vip); SetAuthCookie(new CurrentVipModel { OpenId = openId, VipId = vip.Id, VipName = vip.VipName, pwd = EncryptHelper.Md5(vip.Password) }); return(RedirectToAction("Index", "User")); } else { return(RedirectToAction("Login", "Account")); } } else { return(RedirectToAction("Login", "Account")); } //根据openId获取用户,如果没有找到就跳转注册页面,注册过就设置cookie,以后免登陆 //var vip = _dal.GetVipByOpenId(openId); //if (vip == null) //{ // return RedirectToAction("Mobile", "Register", new { openId = openId }); //} //else //{ // SetAuthCookie(new CurrentVipModel // { // OpenId = openId, // VipId = vip.Id, // VipName = vip.VipName // }); // return RedirectToAction("Index", "Home"); //} } catch (Exception ex) { return(Content(ex.Message)); } }