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 否则从cookie取值
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 Then'说明授权成功,不管是从code授权码获取的还是从cookie获取的
Verified = True
ElseIf e.GetValues.ContainsKey("code") = False Then '如果授权失败,说明即没有code授权码,也没有cookie值
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