最近一段时间一直在作关于微信方面的网站应用开发,这段时间也收获的很多关于微信开发方面的开发技能,接触的比较多的主要有微信公众号和微信网站app第三方登陆受权,以及微信会员卡,优惠券和扫描二位码的功能,今天我主要想要总结的是微信公众号登陆和网站app第三方应用微信受权登陆这二者之间获取到的Openid关联问题,实现两边登陆都是同一个帐号。web
首先咱们必须区别开来微信公众平台开发是指微信公众号进行业务开发(https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432),为网站微信登陆受权是须要在微信开发平台中建立网站应用来使用的(https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN),可是要想把微信公众号于微信网站受权登陆获取到的Oppenid关联起来的话咱们能够经过UnionID关联起来。json
微信开发平台公众帐号关联的以下图:api
开发者可经过OpenID来获取用户基本信息。特别须要注意的是,若是开发者拥有多个移动应用、网站应用和公众账号,可经过获取用户基本信息中的unionid来区分用户的惟一性,由于只要是同一个微信开放平台账号下的移动应用、网站应用和公众账号,用户的unionid是惟一的。换句话说,同一用户,对同一个微信开放平台下的不一样应用,unionid是相同的。安全
获取用户基本信息(包括UnionID机制)开发者可经过OpenID来获取用户基本信息。请使用https协议。服务器
接口调用请求说明 http请求方式: GET https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
微信网站用户赞成受权获取code的api:微信
第三方使用网站应用受权登陆前请注意已获取相应网页受权做用域(scope=snsapi_login),则能够经过在PC端打开如下连接: https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect 若提示“该连接没法访问”,请检查参数是否填写错误,如redirect_uri的域名与审核时填写的受权域名不一致或scope不为snsapi_login。
//受权入口 public ActionResult WxLogin() { //异步跳转地址 var loginUrl = "http://" + Request.Url.Authority + Url.Action("WxRegisterAndLogin") ; return Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + Url.Encode(loginUrl) + "&response_type=code&scope=snsapi_userinfo&state=state#wechat_redirect"); }
/// <summary> /// 微信登陆注册(经过unionid来判断以前是否已经存在同一个工做平台注册过的帐号,假如存在的话则关联起来,不存在注册一个新的帐号) /// </summary> /// <param name="code">获取用户凭证换取用户网页受权</param> /// <param name="ReturnUrl">跳转地址</param> /// <returns></returns> public async Task<ActionResult> WxRegisterAndLogin(string code, string ReturnUrl = "") { try { //登陆成功后跳转的地址 string url=ReturnUrl; //经过Code以及微信appscrect和wxappid换取网页受权access_token和用户oppenid HttpClient webClient = new HttpClient(); var jsonString = await (await webClient.GetAsync("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +公众号appid+ "&secret=" + 公众号AppSecret + "&code=" + code + "&grant_type=authorization_code")).Content.ReadAsStringAsync(); //异步获取的用户oppenid和access_token var jsonOAuthorObj = JsonConvert.DeserializeObject(jsonString, new { access_token = "", openid = "" }.GetType()); if (jsonOAuthorObj.openid == null) { return Content(jsonString + "出现错误请重试"); } var myuser = new user { WxOpenId = jsonOAuthorObj.openid }.SelectObject(); //注册成功后直接登陆,受权会判断是否有帐户 if (myuser == null) { //拉取用户信息(需scope为 snsapi_userinfo),和unionid(只有在用户将公众号绑定到微信开放平台账号后,才会出现该字段) jsonString = await (await webClient.GetAsync("https://api.weixin.qq.com/sns/userinfo?access_token=" + jsonOAuthorObj.access_token + "&openid=" + jsonOAuthorObj.openid + "&lang=zh_CN")).Content.ReadAsStringAsync(); dynamic jsonObj = JsonConvert.DeserializeObject(jsonString, new { nickname = "", headimgurl = "", sex = "", openid = "", country = "", province = "", city = "",unionid=""}.GetType()); //查询系统中是否存在unionid用户信息,若存在则更新当前用户openid,并直接登陆,若是不存在的话则须要建立一个新的用户信息 var isExistUserInfo=new user(){unionid=unionid }.SelectObject(); if(isExistUserInfo!=null)//存在该用户记录 { //更新公众号openid isExistUserInfo.WxOpenId=jsonObj.openid; isExistUserInfo.Update(); //存在用户信息直接登陆 return Redirect(url); } else//不存在该用户记录 { //建立用户 int cUserId = new user { Wximage= jsonObj.headimgurl, WxNickName = jsonObj.nickname, WxOpenId = jsonObj.openid, Sex = Convert.ToInt32(jsonObj.sex), Country = jsonObj.country, Province = jsonObj.province, City = jsonObj.city,unionid=unionid }.Create(); return RedirectToAction("WxRegister", "Login", new { ReturnUrl = url }); } } else { //存在用户信息直接登陆 return Redirect(url); } } catch (Exception e) { return View("MessageInfo", "", e.ToString()); } }