以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 微信授权页面一直没成功 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=179753) |
||||||
-- 作者:xuzihang -- 发布时间:2022/9/7 12:18:00 -- 微信授权页面一直没成功 Dim sb As New StringBuilder sb.AppendLine("<meta name=\'viewport\' c>") If e.host = "w**.1**.cc" 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, "wx3c7dbe******", "4eb1d20dce6820b6218f376a6cb***", 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 " \'根据openid和accesstoken获取用户详情,注意这里这个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://w**.1**.cc\'>刷新页面</a>") Else sb.AppendLine("你无权访问系统") End If End If e.WriteString(sb.ToString)
|
||||||
-- 作者:xuzihang -- 发布时间:2022/9/7 12:20:00 -- 还有配置别的么? |
||||||
-- 作者:有点蓝 -- 发布时间:2022/9/7 13:34:00 -- Dim ul As String = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8acdb7df5beb68fd&redirect_uri=http%3a%2f%2f这里改为自己要授权的网页地址wexin.foxtable.com&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect" 简单网页授权 的一般流程: 1、将目标网页的地址包装在一个授权链接中,这个链接是指向腾讯的授权服务器的,格式如下:
2、用户打开目标网页,后台在cookie中检查是否存在登录信息,如果存在,直接显示目标网页内容。 |