这篇主要是承接上篇的网页受权获取用户基本信息的后文,也是对第一种静默受权以后,用户点击公众号内连接时,如何再次取得当前用户的OpenId的大体讲解和一些注意事项。html
看过上一篇的小伙伴都知道,咱们在用户关注的时候就已经将该用户的基本信息存入数据库中,那么若是该用户过了好久才点击公众号内的网页连接,那么咱们该如何再次获取这个惟一标识呢?数据库
具体实现api
首先,咱们定一个获取openid的方法 ReGetOpenId服务器
public static void ReGetOpenId() { string url = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;//获取当前url if (System.Web.HttpContext.Current.Session["openid"] == "" || System.Web.HttpContext.Current.Session["openid"] == null) { //先要判断是不是获取code后跳转过来的 if (System.Web.HttpContext.Current.Request.QueryString["code"] == "" || System.Web.HttpContext.Current.Request.QueryString["code"] == null) { //Code为空时,先获取Code string GetCodeUrls = GetCodeUrl(url); System.Web.HttpContext.Current.Response.Redirect(GetCodeUrls);//先跳转到微信的服务器,取得code后会跳回来这页面的 } else { //Code非空,已经获取了code后跳回来啦,如今从新获取openid Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); string openid = ""; openid = GetOauthAccessOpenId(System.Web.HttpContext.Current.Request.QueryString["Code"]);//从新取得用户的openid System.Web.HttpContext.Current.Session["openid"] = openid; } } }
注:url最好是带域名的,花生壳的域名是行不通的,再调微信平台接口的时候,会报连接不正确错误微信
上文中GetCodeUrl方法以下app
#region 从新获取Code的跳转连接(没有用户受权的,只能获取基本信息) /// <summary>从新获取Code,之后面实现带着Code从新跳回目标页面(没有用户受权的,只能获取基本信息(openid))</summary> /// <param name="url">目标页面</param> /// <returns></returns> public static string GetCodeUrl(string url) { string CodeUrl = ""; //对url进行编码 url = System.Web.HttpUtility.UrlEncode(url); CodeUrl = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + Appid + "&redirect_uri=" + url + "?action=viewtest&response_type=code&scope=snsapi_base&state=1#wechat_redirect"); return CodeUrl; } #endregion
上文中 GetOauthAccessOpenId方法以下post
#region 以Code换取用户的openid、access_token /// <summary>根据Code获取用户的openid、access_token</summary> public static string GetOauthAccessOpenId(string code) { Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); string Openid = ""; string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Appid + "&secret=" + Secret + "&code=" + code + "&grant_type=authorization_code"; string gethtml = MyHttpHelper.HttpGet(url); log.log("拿到的url是:" + url); log.log("获取到的gethtml是" + gethtml); OAuth_Token ac = new OAuth_Token(); ac = JsonHelper.ToObject<OAuth_Token>(gethtml); log.log("可否从html里拿到openid=" + ac.openid); Openid = ac.openid; return Openid; } #endregion
经过以上方法便可拿到用户的Openid,如上文所示,用户id保存在System.Web.HttpContext.Current.Session["openid"] 中,因此获取也是很是简单编码
在须要获取的地方执行url
#region 获取当前用户Openid ReGetOpenId(); log.log("走完获取openid的方法以后,当前Session的值是:" + System.Web.HttpContext.Current.Session["openid"]); #endregion
注:上文中 OAuth_Token 类以下:spa
public class OAuth_Token { /// <summary> /// 网页受权接口调用凭证,注意:此access_token与基础支持的access_token不一样 /// </summary> public string access_token { get; set; } /// <summary> /// access_token接口调用凭证超时时间,单位(秒) /// </summary> public string expires_in { get; set; } /// <summary> /// 用户刷新access_token /// </summary> public string refresh_token { get; set; } /// <summary> /// 用户惟一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号惟一的OpenID /// </summary> public string openid { get; set; } /// <summary> /// 用户受权做用域 /// </summary> public string scope { get; set; } }
由于没法使用通常的url,因此是把程序部署在服务器上,没法进行调试,只有打印日志查看效果,最后点击连接,日志以下:
用到的简单日志类也顺便提供放上来:
/// <summary> /// 日志类 /// </summary> public class Log { private string logFile; private StreamWriter writer; private FileStream fileStream = null; public Log(string fileName) { logFile = fileName; CreateDirectory(logFile); } public void log(string info) { try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(logFile); if (!fileInfo.Exists) { fileStream = fileInfo.Create(); writer = new StreamWriter(fileStream); } else { fileStream = fileInfo.Open(FileMode.Append, FileAccess.Write); writer = new StreamWriter(fileStream); } writer.WriteLine(DateTime.Now + ": " + info); } finally { if (writer != null) { writer.Close(); writer.Dispose(); fileStream.Close(); fileStream.Dispose(); } } } public void CreateDirectory(string infoPath) { DirectoryInfo directoryInfo = Directory.GetParent(infoPath); if (!directoryInfo.Exists) { directoryInfo.Create(); } } }
调用呢,很简单,调用方法以下:
Log log = new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt"); log.log("我会被输入在日志文件中")
最后呢,拿到当前用户Openid,就能够从数据库再次获取到该用户的其余基本信息。从而能够更好的辅助你完成你项目中其余的业务模块。
未完待续,持续填坑中。。。