protected void Page_Load(object sender, EventArgs e) { LoginConfig loginConfig = new LoginConfig(); string app_id = loginConfig.AppKey; string app_secret = loginConfig.AppSecret; //成功授权后的回调地址 string redirect_url = Server.UrlEncode("http://" + Request.Url.Host + "/Plugins/Login/Weixin/WeixinLogin.aspx"); string _state = new Random().Next(0, 9999).ToString(); //微信授权登录 scope:snsapi_userinfo //微信网页应用 scope:snsapi_login string weixin_url = "https://open.weixin.qq.com/connect/qrconnect?appid=" + app_id + "&redirect_uri=" + redirect_url + "&response_type=code&scope=snsapi_login&state=" + _state + "#wechat_redirect"; Response.Write("<script> location.href='" + weixin_url + "'</script>"); Response.End(); }
protected void Page_Load(object sender, EventArgs e) { LoginConfig loginConfig = new LoginConfig(); this.Appid = loginConfig.AppKey; this.Appsecret = loginConfig.AppSecret; this.code = RequestHelper.GetQueryString <string>("code"); if (!string.IsNullOrEmpty(code)) { #region 获取access_token Access_tokenResult access_tokenResult = new Access_tokenResult(); //如果session有值 if (Session["expires_in"] != null && Session["access_token"] != null && Session["openid"] != null) {//如果Session["access_token"]没过期 if (Convert.ToInt32(Session["expires_in"]) >= ShopCommon.ConvertDateTimeInt(DateTime.Now)) { access_tokenResult.access_token = Session["access_token"].ToString(); access_tokenResult.openid = Session["openid"].ToString(); } else//如果Session["access_token"]过期,根据refresh_token刷新access_token { if (Session["refresh_token"] != null) { access_tokenResult = RefreshAccess_token(Session["refresh_token"].ToString()); } } } else { //根据code获取access_token access_tokenResult = CodeGetOpenidAndAccess_token(this.code); if (!string.IsNullOrEmpty(access_tokenResult.errcode) || string.IsNullOrEmpty(access_tokenResult.access_token) || string.IsNullOrEmpty(access_tokenResult.openid)) { Response.Write("参数错误,请稍后重试"); Response.End(); } else { //验证access_token是否有效,失效了重新获取 WeChatMsg _msg = Check_Access_token(access_tokenResult.access_token, access_tokenResult.openid); if (_msg.errcode != "0") { Response.Write("参数错误,请稍后重试"); Response.End(); } else {//access_token有效,重新赋值session Session["expires_in"] = ShopCommon.ConvertDateTimeInt(DateTime.Now) + 7000; Session["access_token"] = access_tokenResult.access_token; Session["refresh_token"] = access_tokenResult.refresh_token; Session["openid"] = access_tokenResult.openid; } } } #endregion //获取userinfo Snsapi_userinfo snsapi_userinfo = GetUserinfo(access_tokenResult.access_token, access_tokenResult.openid); if (!string.IsNullOrEmpty(snsapi_userinfo.errcode) || string.IsNullOrEmpty(snsapi_userinfo.openid)) { Response.Write("参数错误,请稍后重试"); Response.End(); } else { string openID = snsapi_userinfo.openid; //string openID = access_tokenResult.openid; openID = "wx-" + openID; //如果没有用户添加用户 int userID = UserBLL.Read(openID).Id; UserInfo userInfo = new UserInfo(); if (userID <= 0) { userInfo.UserName = Server.UrlDecode(snsapi_userinfo.nickname); userInfo.UserPassword = StringHelper.Password(Guid.NewGuid().ToString(), (PasswordType)ShopConfig.ReadConfigInfo().PasswordType); userInfo.Photo = snsapi_userinfo.headimgurl; int _sex = userInfo.Sex; int.TryParse(snsapi_userinfo.sex, out _sex); //微信sex:1 男,2 女, 0 未知 //本站sex:1 男,2 女, 3 未知 userInfo.Sex = _sex == 0 ? 3 : _sex; userInfo.Email = ""; userInfo.RegisterIP = ClientHelper.IP; userInfo.RegisterDate = RequestHelper.DateNow; userInfo.LastLoginIP = ClientHelper.IP; userInfo.LastLoginDate = RequestHelper.DateNow; userInfo.FindDate = RequestHelper.DateNow; userInfo.Status = (int)UserStatus.Normal; userInfo.OpenId = openID; userID = UserBLL.Add(userInfo); } //当前用户登录 userInfo = UserBLL.Read(userID); UserBLL.UserLoginInit(userInfo); userInfo.LastLoginIP = ClientHelper.IP; userInfo.LastLoginDate = RequestHelper.DateNow; UserBLL.Update(userInfo); //跳转至会员中心 Response.Write("<script> location.href='/Mobile/User/Index.html'</script>"); Response.End(); //ResponseHelper.Redirect("/User/Index.html"); } } }