protected void Page_Load(object sender, EventArgs e) { this.returnUrl = WEBRequest.GetQueryString("ReturnUrl"); LoginType = CookieHelper.GetCookie("WXLoginType"); code = WEBRequest.GetQueryString("code"); //微信登录时从cookie中没法取到 if (LoginType.ToText().IsEmpty()) { LoginType = WEBRequest.GetQueryString("state"); CookieHelper.WriteCookie("WXLoginType", LoginType); } if (returnUrl.ToText().IsEmpty()) { returnUrl = "/Member/my_account.aspx"; } if (code.ToText() == "") { //授权失败时返回到登录框 Response.Redirect("/user/Login.aspx?ReturnUrl=" + returnUrl); return; } //判断当前第三方帐号是否有绑定过,若有绑定过直接登录。 ThirdLoginSDK shareSdk = new ThirdLoginSDK(); shareSdk.InitSDK(ThirdLoginSDK.GetLoginType(LoginType.ToString())); //根据code获取第三方用户信息 ThirdLoginSDK.ThirdUserInfo mUserInfo = shareSdk.GetUserSampleInfo(code); if (LoginType.ToInt(0) == (int)ThirdLoginSDK.ThirdLoginType.QQ) { CookieHelper.WriteCookie("OpenId", Tool.Cryptography.TripleDESEncrypt(mUserInfo.UId)); } else if (LoginType.ToInt(0) == (int)ThirdLoginSDK.ThirdLoginType.Sina) { CookieHelper.WriteCookie("weiboUserId", mUserInfo.UId); } else if (LoginType.ToInt(0) == (int)ThirdLoginSDK.ThirdLoginType.WeiXin) { CookieHelper.WriteCookie("weixinUserId", mUserInfo.UId); } CookieHelper.WriteCookie("ThirdLoginUserInfo", JsonHelper.ToJson(mUserInfo)); UserBLL userbll = new UserBLL(); SqlParameter[] paramData = new SqlParameter[] { new SqlParameter("@ThirdPartyId", mUserInfo.UId) }; UserBasicInfoInfo model = userbll.WXGetUserBasicInfo("ThirdPartyId=@ThirdPartyId", paramData); if (model != null) { //先前已绑定过,就直接登录 WXRegister.UserLogin(model); Response.Redirect(this.returnUrl); return; } }
protected void Page_Load(object sender, EventArgs e) { this.returnUrl = WEBRequest.GetQueryString("ReturnUrl"); LoginType = ThirdLoginSDK.GetLoginType(CookieHelper.GetCookie("WXLoginType")); code = WEBRequest.GetQueryString("code"); if (this.returnUrl.IsEmpty()) { this.returnUrl = "/Member/my_account.aspx"; } ThirdLoginSDK shareSdk = new ThirdLoginSDK(); UserBLL userbll = new UserBLL(); //初始化SDK shareSdk.InitSDK(LoginType); //根据code获取第三方用户信息 ThirdLoginSDK.ThirdUserInfo mUserInfo = shareSdk.GetCacheUserInfo(); string strUId = shareSdk.GetCacheUId(); SqlParameter[] paramData = new SqlParameter[] { new SqlParameter("@ThirdPartyId", strUId) }; UserBasicInfoInfo model = userbll.WXGetUserBasicInfo("ThirdPartyId=@ThirdPartyId", paramData); if (model != null) { //当已存在时直接登录,并跳转 WXRegister.UserLogin(model); Response.Redirect(this.returnUrl); return; } else { //用第三登录信息注册一新帐号 Guid userid = Guid.NewGuid(); var userbasicEntity = WXRegister.AddUserInfo(userid, "tuandai_weixin", "", "", "", mUserInfo.NickName, mUserInfo.HeadImg, mUserInfo.UId, (int)LoginType); if (userbasicEntity != null) { model = userbll.GetUserBasicInfoModelById(userid); WXRegister.UserLogin(model); Response.Redirect(this.returnUrl); return; } } Response.Redirect("/user/Login.aspx?ReturnUrl=" + this.returnUrl); }