以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  关于网页授权  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=117919)

--  作者:客家阿哥
--  发布时间:2018/4/20 19:47:00
--  关于网页授权
Dim e As RequestEventArgs = args(0)

Dim UserId As String
Dim sb As New StringBuilder

If e.GetValues.ContainsKey("code") Then \'如果通过授权链接跳转而来,就根据传递过来的code参数调用接口,获取用户的UserId
    Dim ul As String  = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token={0}&code={1}"
    ul = CExp(ul,Functions.Execute("GetQYAccessToken"),e.GetValues("code"))
    Dim hc As new HttpClient(ul)
    Dim jo As JObject = JObject.Parse(hc.GetData)
    MessageBox.Show("a")
    If jo("UserId") IsNot Nothing Then
        UserId = jo("UserId")
    End If
Else
   
    UserId = e.Cookies("userid") \'否则从cookie中提取userid和username
End If
If UserId  > "" then\'授权成功
    e.AppendCookie("userid",UserId) \'将userid和username存储在Cookie中

    Return UserId
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=SCOPE&agentid=AGENTID&state=STATE#wechat_redirect"

    \'Dim ul2 As String = UrlEncode("http://www.whtcxx.com")
    \'ul1 = CExp(ul1,"wxc919804411b0d36c",ul2)
    \'sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接

    Dim ul As String  = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc919804411b0d36c&redirect_uri=http%3a%2f%2fwww.whtcxx.com&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
   
    sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
    e.WriteString(sb.ToString)

    Return ""
End If
---------------------------------------------------------------




http://www.whtcxx.com/?code=011zW6e92AT00S0Z01d92PCbe92zW6e7&state=123

每次卡在这里网页就不动了



[此贴子已经被作者于2018/4/20 19:47:18编辑过]

--  作者:有点蓝
--  发布时间:2018/4/20 20:25:00
--  
你是企业微信?还是公众号?用法不一样的


--  作者:客家阿哥
--  发布时间:2018/4/20 20:42:00
--  
服务号
--  作者:有点蓝
--  发布时间:2018/4/20 20:51:00
--  
1楼是企业微信的用法



--  作者:客家阿哥
--  发布时间:2018/4/21 0:10:00
--  
仍然卡在这里

http://www.whtcxx.com/?code=021er2pY0at0432VHSoY0bl6pY0er2p3&state=123

--  作者:客家阿哥
--  发布时间:2018/4/21 0:11:00
--  
Dim e As RequestEventArgs = args(0)
Dim sb As New StringBuilder
sb.AppendLine("<meta name=\'viewport\' c>")
If e.host = "www.whtcxx.com" Then \'授权测试
    Dim UserName As String
    Dim OpenID As String
MessageBox.Show("A")
    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,"wxc919804411b0d36c","8157869b6771df1efec88b1f01726b8c",e.GetValues("code"))
        Dim hc As new HttpClient(ul)
        Dim jo As JObject = JObject.Parse(hc.GetData)
MessageBox.Show("B")
        If jo("openid") IsNot Nothing Then \'如果获取openid成功(成功的话,还会同时返回一个accesstiken,用于获取用户详情)
            OpenID = jo("openid")
            Dim dr As DataRow = DataTables("WXUsers").Find("openid =\'" & Openid & "\'")
MessageBox.Show("C")
            If dr IsNot Nothing Then
                UserName = dr("nickname")
MessageBox.Show("D")
            Else
                ul = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN "
                \'根据openid和accesstoken获取用户详情,注意这里这个accesstoken不是普通accesston,只能用于网页授权
                hc = New HttpClient(CExp(ul, jo("access_token"), OpenId))
                jo = jo.Parse(hc.GetData)
MessageBox.Show("E")
                If jo("openid") IsNot Nothing Then
                    UserName = jo("nickname")
                    dr = DataTables("WXUsers").AddNew()
MessageBox.Show("F")
                    Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl"} \'""
                    For Each nm As String In nms
                        dr(nm) = jo(nm)
                    Next
                    dr.Save
                Else
MessageBox.Show("G")
                    e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
                    Return ""
                End If
            End If
            e.AppendCookie("username",UserName) \'用户名和openid存储在Cookie中
            e.AppendCookie("openid",OpenID)
MessageBox.Show("H")
        Else
            e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
            Return ""
        End If
    Else
MessageBox.Show("I")
        UserName = e.Cookies("username") \'从cookie获取用户名和openid
        OpenID = e.Cookies("openid")
        If userName = "" OrElse OpenID = "" Then
MessageBox.Show("J")
            Dim ul As String  = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc919804411b0d36c&redirect_uri=http%3a%2f%2fwww.whtcxx.com&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
            sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
            e.WriteString(sb.ToString)
            Return ""
        End If
    End If
MessageBox.Show("L")
    If  OpenID > "" And UserName > "" Then
        \'这里可以做进一步的权限判断
        sb.AppendLine("欢迎" & UserName & "光临, <a href=\'http://wexin.foxtable.com\'>刷新页面</a>")
    Else
        sb.AppendLine("你无权访问本系统")
    End If
End If
e.WriteString(sb.ToString)

--  作者:客家阿哥
--  发布时间:2018/4/21 0:12:00
--  
中间有授权确认界面出现
--  作者:客家阿哥
--  发布时间:2018/4/21 9:01:00
--  
httprequest
------------------------------

                    Dim fl As String = "d:\\web\\" & e.path
                    If filesys.FileExists(fl)
                        Dim idx As Integer = fl.LastIndexOf(".")
                        Dim ext As String = fl.SubString(idx)
                        Select Case ext
                            Case  ".jpg",".gif",".png",".bmp",".wmf",".js",".css" ,".html",".htm",".zip",".rar"
                                e.WriteFile(fl)
                                Return  \'这里必须返回
                            End  Select
                        End  If
                        Select Case e.Path
                           

                            Case "kaipiaoyuyuetianjia.htm"

                             
                                Functions.Execute("rrzz",e)

                                Functions.Execute("kaipiaoyuyuetianjia",e)

                            End  Select

--  作者:客家阿哥
--  发布时间:2018/4/21 9:08:00
--  
Dim e As RequestEventArgs = args(0)
Dim sb As New StringBuilder
sb.AppendLine("<meta name=\'viewport\' c>")
If e.host = "www.whtcxx.com" Then \'授权测试
    Dim UserName As String
    Dim OpenID As String
MessageBox.Show("A")
    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,"wxc919804411b0d36c","8157869b6771df1efec88b1f01726b8c",e.GetValues("code"))
        Dim hc As new HttpClient(ul)
        Dim jo As JObject = JObject.Parse(hc.GetData)
MessageBox.Show("B")
        If jo("openid") IsNot Nothing Then \'如果获取openid成功(成功的话,还会同时返回一个accesstiken,用于获取用户详情)
            OpenID = jo("openid")
            Dim dr As DataRow = DataTables("WXUsers").Find("openid =\'" & Openid & "\'")
MessageBox.Show("C")
            If dr IsNot Nothing Then
                UserName = dr("nickname")
MessageBox.Show("D")
            Else
                ul = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN "
                \'根据openid和accesstoken获取用户详情,注意这里这个accesstoken不是普通accesston,只能用于网页授权
                hc = New HttpClient(CExp(ul, jo("access_token"), OpenId))
                jo = jo.Parse(hc.GetData)
MessageBox.Show("E")
                If jo("openid") IsNot Nothing Then
                    UserName = jo("nickname")
                    dr = DataTables("WXUsers").AddNew()
MessageBox.Show("F")
                    Dim nms() As String = {"openid","nickname","sex","city","country","province","headimgurl"} \'""
                    For Each nm As String In nms
                        dr(nm) = jo(nm)
                    Next
                    dr.Save
                Else
MessageBox.Show("G")
                    e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
                    Return ""
                End If
            End If
            e.AppendCookie("username",UserName) \'用户名和openid存储在Cookie中
            e.AppendCookie("openid",OpenID)
MessageBox.Show("H")
        Else
            e.WriteString(jo.ToString) \'在用户浏览器显示错误信息
            Return ""
        End If
    Else
MessageBox.Show("I")
        UserName = e.Cookies("username") \'从cookie获取用户名和openid
        OpenID = e.Cookies("openid")
        If userName = "" OrElse OpenID = "" Then
MessageBox.Show("J")
            Dim ul As String  = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxc919804411b0d36c&redirect_uri=http%3a%2f%2fwww.whtcxx.com&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
            sb.Append("<meta http-equiv=\'Refresh\' c>") \'跳转到授权链接
            e.WriteString(sb.ToString)
            Return ""
        End If
    End If
MessageBox.Show("L")
    If  OpenID > "" And UserName > "" Then
        \'这里可以做进一步的权限判断
        sb.AppendLine("欢迎" & UserName & "光临, <a href=\'http://wexin.foxtable.com\'>刷新页面</a>")
    Else
        sb.AppendLine("你无权访问本系统")
    End If
End If
e.WriteString(sb.ToString)

-----------------------------------
把红色这一段删掉,则跳转到http://www.whtcxx.com/kaipiaoyuyuetianjia.htm,但页面是一片空白

--  作者:有点蓝
--  发布时间:2018/4/21 10:27:00
--  
这2个函数分别有什么代码?

Functions.Execute("rrzz",e)

Functions.Execute("kaipiaoyuyuetianjia",e)