HttpRequest代码:
***
If e.host = "www.abc.cn" Then
Dim OpenID As String
Dim wb As New weui
Dim sb As New StringBuilder
sb.AppendLine("<meta name='viewport' c>")
If e.GetValues.ContainsKey("code") Then
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,"wxd01***","541e1c7d***",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")
wb.AppendCookie("openid",OpenID)
'e.AppendCookie("openid",OpenID)
msgbox(openid)
'不管使用e.AppendCookie还是wb.AppendCookie,都能获取(弹出)正确的openid。
'使用wb.AppendCookie,每次登录都会到这里,获取到的openid都不能传递到"dd"页面。
'使用e.AppendCookie,第1次登录无法传递openid到"dd"页面,第2次及后面的登录不会再到这里,因为可以使用第1次的openid,判定【 Verified = True】,所以直达"dd"页面。
End If
Else
OpenId = e.Cookies("openid")
msgbox(OpenId) 'wb.AppendCookie无法获取到openid,e.AppendCookie第2次及后面的登录在这里能获取到openid >> Verified = True
msgbox(e.path) '不管哪个登录,都是 dd.htm
End If
Dim Verified As Boolean
Dim dr As DataRow = DataTables("Users").SQLFind("[openid] ='" & OpenID & "' and [离职] = 0")
If OpenId > "" AndAlso dr IsNot Nothing Then
Verified = True
ElseIf e.GetValues.ContainsKey("code") = False Then
Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd01***&redirect_uri=" & UrlEncode(e.Request.URL.ToString) & "&response_type=code&scope=snsapi_base&state=123#wechat_redirect"
sb.Append("<meta http-equiv='Refresh' c o n t e n t='0; url=" & ul & "'>") '返回到上面的授权,去获取openid
e.WriteString(sb.ToString)
Return '必须的
End If
If Verified = False Then
wb.InsertHTML(""& 报错页面 &"") '错误就到这里
e.WriteString(wb.Build) '生成网页
Return '必须的
ElseIf e.path = "" Then '从登录页访问
wb.InsertHTML(""& 报错页面 &"") '错误就到这里
e.WriteString(wb.Build) '生成网页
Return '必须的
End If
End If
’不管是wb.AppendCookie还是e.AppendCookie,在这里openid都是存在的,认证都是通过的【Verified = True】,就跳转到下面的"dd"页面。
Select Case e.path '开始生成网页
Case "dd.htm"
Functions.Execute("dd",e)
End select
2、"dd"内部函数代码:
Dim e As RequestEventArgs = args(0)
Dim wb As New WeUI
msgbox(e.Cookies("openid"))
e.WriteString(wb.Build)