以文本方式查看主题

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

--  作者:客家阿哥
--  发布时间:2018/4/12 23:19:00
--  关于网页详情授权

一个简单例子

1、在微信后台将网页授权域名设置为"wexin.foxtable.com",注意不能加"http"。

2、HttpRequest事件代码如下:

Dim sb As New StringBuilder
sb
.AppendLine("<meta name=\'viewport\' c>")
If
 e.host = "wexin.foxtable.com" Then \'授权测试
    Dim UserName As String
    Dim OpenID As String
    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","dec0df8a469518df30e9bdef0758c678",e.GetValues("code"))
        Dim hc As new HttpClient(ul)
        Dim jo As JObject = JObject.Parse(hc.GetData)
        If jo("openid"IsNot Nothing Then \'
如果获取openid成功(成功的话,还会同时返回一个accesstiken,用于获取用户详情)
            OpenID = jo("openid")
            Dim dr As DataRow = DataTables("WXUsers").Find("openid =\'" & Openid & "\'")
            If dr IsNot Nothing Then
                UserName = dr("nickname")
            Else
                ul = "https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN "
                \'
根据openidaccesstoken获取用户详情,注意这里这个accesstoken不是普通accesston,只能用于网页授权
                hc = New HttpClient(CExp(ul, jo("access_token"), OpenId))
                jo = jo.Parse(hc.GetData)
                If jo("openid"IsNot Nothing Then
                    UserName = jo("nickname")
                    dr = DataTables("WXUsers").AddNew()
                    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
                    e.WriteString(jo.ToString) \'
在用户浏览器显示错误信息
                    Return
                End If
            End If
            e.AppendCookie("username",UserName) \'
用户名和openid存储在Cookie
            e.AppendCookie("openid",OpenID)
        Else
            e.WriteString(jo.ToString) \'
在用户浏览器显示错误信息
            Return
        End If
    Else
        UserName = e.Cookies("username"\'
cookie获取用户名和openid
        OpenID = e.Cookies("openid")
        If userName = "" OrElse OpenID = "" 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_userinfo&state=123#wechat_redirect"
            sb.Append("<meta http-equiv=\'Refresh\' c>"\'
跳转到授权链接
            e.WriteString(sb.ToString)
            Return
        End If
    End If
    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/12 23:22:00
--  
最后显示sb.AppendLine("欢迎" & UserName & "光临, <a href=\'http://wexin.foxtable.com\'>刷新页面</a>")这句的内容,点击以后又循环验证一遍
--  作者:有点甜
--  发布时间:2018/4/12 23:42:00
--  

1、改成你自己的地址啊

 

2、改成 sb.AppendLine("欢迎" & UserName & "光临, <a href=\'http://你自己的某个网页\'>刷新页面</a>")

 

3、看懂代码,加入msgbox测试,不理解你说的问题


--  作者:客家阿哥
--  发布时间:2018/4/12 23:57:00
--  
改了自己的网址,要改的都改了        
UserName = e.Cookies("username"\'cookie获取用户名和openid
 OpenID = e.Cookies("openid")

也能正常获得值

但是点击,刷新页面的时候,跳转不到,自己的某个页面,加了msgbox测试,发现,只是把网页详情授权代码,重新执行了一遍

--  作者:客家阿哥
--  发布时间:2018/4/13 0:10:00
--  
可以了,和其它网页代码顺序搞错了图片点击可在新窗口打开查看