Rss & SiteMap
Foxtable(狐表) http://www.foxtable.com
我现在这个需求是,浏览这个网页时,可能在微信中,也可能是手机的浏览器,如果是在微信中浏览,我要获取当前用户 的openid,并且保存在对应的用户资料中。如果不是微信中浏览,就不用管,正常操作就行。
所以我这个思路可行不,如果是在微信里,则获取授权,如果不在微信里,则正常浏览
If e.host = "wexin.foxtable.com" Then '需要授权才能访问的域名
Dim OpenID As String
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.GetValues.ContainsKey("code") Then '如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的OpenID
Dim ul As String = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
ul = CExp(ul,"wx8acdb7df5beb68fd","a5a3b06a790d4200a151e6b13c3dc263",e.GetValues("code"))
Dim hc As new HttpClient(ul)
Dim jo As JObject = JObject.Parse(hc.GetData)
If jo("openid") IsNot Nothing Then
OpenID = jo("openid")
e.AppendCookie("openid",OpenID) '将openid存储在Cookie中
End If
Else
OpenId = e.Cookies("openid") '否则从cookie中提取openid
End If
Dim Verified As Boolean
Dim dr As DataRow = DataTables("WXUsers").Find("openid ='" & OpenID & "'") '根据openid找出对应的行
If OpenId > "" AndAlso dr IsNot Nothing AndAlso dr("permit") = True '授权成功
Verified = True
ElseIf e.GetValues.ContainsKey("code") = False Then '如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8acdb7df5beb68fd&redirect_uri=http%3a%2f%2fwexin.foxtable.com&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
Return
End If
If Verified = False Then
sb.AppendLine("你无权访问本系统")
Else
sb.AppendLine("Foxtable欢迎您, <a href='http://wexin.foxtable.com'>刷新页面</a>")
End If
e.WriteString(sb.ToString)
End If
帮助中“简单网页授权”中的这段代码,我标注黄色这句不是太理解,访问这个网页时,如果从上至下运行,开始肯定是没有包含code,那么也e.Cookies中为什么会包含openid了呢?
Dim openid As String
If e.Request.Headers("User-Agent").ToLower.Contains("micromessenger") Then '如果是微信登录
Dim dropenid As DataRow = DataTables("登录用户").sqlfind("用户姓名='" & e.Cookies("用户姓名") & "'")
If dropenid IsNot Nothing Then
If dropenid("openid") = "" Then '假如没有openid
openid = Functions.Execute("getopenid", e)
dropenid("openid") = openid
dropenid.Save()
End If
End If
End If
此主题相关图片如下:img_9490.png
在微信浏览器中浏览,提示这个。
getopenid函数:
Dim e As RequestEventArgs = args(0)
Dim url As String = e.Request.URL.Tostring
messagebox.Show(url)
url = url.Replace(":8003", "") '此处将8003去掉,只服务器设置了nginx时需要这样做
Dim OpenID As String
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.GetValues.ContainsKey("code") Then '如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的OpenID
Dim ul As String = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
ul = CExp(ul, "xxx", "xxx", e.GetValues("code"))
Dim hc As New HttpClient(ul)
Dim jo As JObject = JObject.Parse(hc.GetData)
If jo("openid") IsNot Nothing Then
OpenID = jo("openid")
e.AppendCookie("openid", OpenID) '将openid存储在Cookie中
End If
Else
OpenId = e.Cookies("openid") '否则从cookie中提取openid
End If
Dim Verified As Boolean
If OpenId > "" Then '授权成功
Verified = True
Return OpenId
ElseIf e.GetValues.ContainsKey("code") = False Then '如果授权失败,且不是通过授权链接跳转而来,那么就跳转到授权链接
Dim ul1 As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state={2}#wechat_redirect"
Dim ul2 As String = UrlEncode(url)
ul1 = CExp(ul1, "xxx", ul2, "123")
sb.Append("<meta http-equiv='Refresh' c>") '跳转到授权链接
e.WriteString(sb.ToString)
Return ""
End If
If Verified = True Then
Return OpenId
End If