private void RegisterUser(string wxopenid, string wxNickName, string clientIP, string userName, string nickName, string password, string alipayAccount, string alipayRealName, string IDCardNo, string email, string qq) { //invitationCode = Session["ic"] as string; string invitationCode = ""; int result = WcfClient.Instance.RegisterUserFromWeiXin(wxopenid, wxNickName, clientIP, userName, nickName, password, alipayAccount, alipayRealName, IDCardNo, email, qq, invitationCode); if (result == OperResult.RESULTCODE_TRUE) { var player = WcfClient.Instance.GetPlayerByWeiXinOpenID(wxopenid); if (player != null) { WebUserInfo userinfo = new WebUserInfo(); userinfo.xlUserID = player.SimpleInfo.UserID; userinfo.xlUserName = player.SimpleInfo.UserName; userinfo.wxOpenID = wxopenid; // 登录状态100分钟内有效 MyFormsPrincipal <WebUserInfo> .SignIn(userinfo.xlUserName, userinfo, 100); Session[userinfo.xlUserName] = player; } Response.Redirect("View/Index.aspx"); //Response.Write("<script>alert('恭喜您成功加入灵币矿场!');this.location.href='View/Index.aspx';</script>"); } else { Response.Write("<script>alert('注册失败, 原因为:" + OperResult.GetMsg(result) + "')</script>"); } }
//检查用户名和密码匹配 public JsonResult CheckUser(string userName, string password) { SysUser user; if ((user = userService.CheckUser(userName, password)) != null) { //FormsAuthentication.SetAuthCookie(userName, true); MyFormsPrincipal <SysUser> .SignIn(user.UserName, user, 60); return(Json(new ResultModel() { Result = true, ResultInfo = "登录成功" })); } return(Json(new ResultModel() { Result = false, ResultInfo = "用户名或密码错误请重新输入" })); }
protected void btnBind_Click(object sender, EventArgs e) { try { string userName = this.txtUserName.Text.Trim(); string password = this.txtPassword.Text; if (userName == "") { Response.Write("<script>alert('请输入用户名')</script>"); return; } if (password == "") { Response.Write("<script>alert('请输入密码')</script>"); return; } #if Test WeiXinUserInfoModel userObj = null; if (userName == "小开心") { userObj = new WeiXinUserInfoModel() { openid = Config.TestUserOpenId, nickname = "小查", }; } else if (userName == "nero") { userObj = new WeiXinUserInfoModel() { openid = Config.TestLVSU_UserOpenID, nickname = "wgflicker", }; } Session[Config.SESSIONKEY_WXUSERINFO] = userObj; string ip = System.Web.HttpContext.Current.Request.UserHostAddress; OperResultObject resultObj = WcfClient.Instance.WeiXinLogin(userObj.openid, userObj.nickname, ip); if (resultObj.OperResultCode == OperResult.RESULTCODE_TRUE) { var player = WcfClient.Instance.GetPlayerByWeiXinOpenID(userObj.openid); WebUserInfo userinfo = new WebUserInfo(); userinfo.xlUserID = player.SimpleInfo.UserID; userinfo.xlUserName = player.SimpleInfo.UserName; userinfo.wxOpenID = userObj.openid; // 登录状态100分钟内有效 MyFormsPrincipal <WebUserInfo> .SignIn(userinfo.xlUserName, userinfo, 100); Session[userinfo.xlUserName] = player; Response.Redirect("View/Index.aspx", false); //Server.Execute("View/Index.aspx"); } else if (resultObj.OperResultCode == OperResult.RESULTCODE_EXCEPTION) { Response.Write("<script>alert('服务器连接失败,请稍候再试')</script>"); } else { string message = string.IsNullOrEmpty(resultObj.Message) ? OperResult.GetMsg(resultObj.OperResultCode) : resultObj.Message; Response.Write("<script>alert('测试登录失败, 原因为:" + message + "')</script>"); } #else WeiXinUserInfoModel wxuserinfo = Session["wxuserinfo"] as WeiXinUserInfoModel; if (wxuserinfo == null) { Response.Write("<script>alert('只能从微信客户端打开')</script>"); return; } if (string.IsNullOrEmpty(wxuserinfo.openid)) { Response.Write("<script>alert('微信登录失败,无法绑定')</script>"); return; } string ip = System.Web.HttpContext.Current.Request.UserHostAddress; OperResultObject resultObj = WcfClient.Instance.BindWeiXinUser(wxuserinfo.openid, wxuserinfo.nickname, userName, password, ip); if (resultObj.OperResultCode == OperResult.RESULTCODE_TRUE) { var player = WcfClient.Instance.GetPlayerByWeiXinOpenID(wxuserinfo.openid); if (player != null) { WebUserInfo userinfo = new WebUserInfo(); userinfo.xlUserID = player.SimpleInfo.UserID; userinfo.xlUserName = player.SimpleInfo.UserName; userinfo.wxOpenID = wxuserinfo.openid; // 登录状态100分钟内有效 MyFormsPrincipal <WebUserInfo> .SignIn(userinfo.xlUserName, userinfo, 100); Session[userinfo.xlUserName] = player; Response.Redirect("View/Index.aspx", false); } else { Response.Write("<script>alert('绑定失败, 原因为:没有找到迅灵账户')</script>"); } } else { string message = string.IsNullOrEmpty(resultObj.Message) ? OperResult.GetMsg(resultObj.OperResultCode) : resultObj.Message; Response.Write("<script>alert('绑定失败, 原因为:" + message + "')</script>"); } #endif } catch (Exception exc) { LogHelper.Instance.AddErrorLog("Bind User Exception", exc); } }
protected void Page_Load(object sender, EventArgs e) { try { if (!this.IsPostBack) { //code说明 : code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。 string code = Request["code"]; string state = Request["state"]; this.lblMsg.Text = "欢迎进入迅灵矿场"; LogHelper.Instance.AddInfoLog("code:" + code + "; state: " + state); if (state == Config.state) { HttpGetReturnModel resultValue = WeiXinHandler.SynGetUserAccessToken(code); if (resultValue.Exception != null) { this.lblMsg.Text = "登录异常,请联系迅灵矿场管理员"; return; } if (resultValue.ResponseError != null) { Session[Config.SESSIONKEY_RESPONSEERROR] = resultValue.ResponseError; Server.Transfer("ErrorPage.aspx"); return; } AuthorizeResponseModel authObj = resultValue.ResponseResult as AuthorizeResponseModel; if (authObj != null) { this.lblMsg.Text = "authObj OK"; Session[Config.SESSIONKEY_AUTHORIZEOBJ] = authObj; resultValue = WeiXinHandler.SyncGetUserInfo(authObj.access_token, authObj.openid); } if (resultValue.Exception != null) { this.lblMsg.Text = "登录异常,请联系迅灵矿场管理员"; return; } if (resultValue.ResponseError != null) { Session[Config.SESSIONKEY_RESPONSEERROR] = resultValue.ResponseError; Server.Transfer("ErrorPage.aspx"); return; } WeiXinUserInfoModel userObj = resultValue.ResponseResult as WeiXinUserInfoModel; Session[Config.SESSIONKEY_WXUSERINFO] = userObj; string ip = System.Web.HttpContext.Current.Request.UserHostAddress; this.lblMsg.Text = "欢迎 " + userObj.nickname + " 进入迅灵矿场"; OperResultObject resultobj = WcfClient.Instance.WeiXinLogin(userObj.openid, userObj.nickname, ip); this.lblMsg.Text = "登录迅灵矿场,结果为:" + OperResult.GetMsg(resultobj.OperResultCode); if (resultobj.OperResultCode == OperResult.RESULTCODE_TRUE) { this.lblMsg.Text = "WeiXinLogin OK"; var player = WcfClient.Instance.GetPlayerByWeiXinOpenID(userObj.openid); this.lblMsg.Text = "player OK"; WebUserInfo userinfo = new WebUserInfo(); userinfo.xlUserID = player.SimpleInfo.UserID; userinfo.xlUserName = player.SimpleInfo.UserName; userinfo.wxOpenID = userObj.openid; // 登录状态100分钟内有效 MyFormsPrincipal <WebUserInfo> .SignIn(userinfo.xlUserName, userinfo, 100); //Session[userinfo.xlUserName] = player; Response.Redirect("View/Index.aspx", false); } else if (resultobj.OperResultCode == OperResult.RESULTCODE_USER_NOT_EXIST || resultobj.OperResultCode == OperResult.RESULTCODE_USERNAME_PASSWORD_ERROR) { Response.Redirect("LoginPage.aspx", false); } else { string message = string.IsNullOrEmpty(resultobj.Message) ? OperResult.GetMsg(resultobj.OperResultCode) : resultobj.Message; Response.Write("<script>alert('登录迅灵矿场失败, 原因为:" + message + "')</script>"); } } else { } } } catch (Exception exc) { this.lblMsg.Text = "WeiXinResponse Exception. " + exc.Message; LogHelper.Instance.AddErrorLog("WeiXinResponse Exception", exc); } }