当前位置:网站首页>Excel上使用VBA的WebBrowser控件实现单点登录(SSO)
Excel上使用VBA的WebBrowser控件实现单点登录(SSO)
2022-06-09 13:44:00 【金木杂谈】
需求
Excel 上可以做出很多漂亮的报表, 产品经理希望能够在Excel上弹出登录页面,实现单点登录,
登录完成后,从服务器端取回模板列表,选择其中一个模板,插入到Excel中。
设计
- 登录界面可用前端技术实现,比如Angular与VUE
- VBA中自带的WebBrowser控件可以作为加载Web登录页面的“壳”。
- VBA不断尝试获取页面上的cookie,直到取到cookie,并且cookie中包含token。
- 使用URLDecode解码cookie,获取token。
- 使用该token发出rest api请求获取模板列表。
部分实现
从WebBrowser获取cookie
Public Function GetCookieFromBrowser(serverIP As String)
Dim url As String
Dim encodedCookie As String
encodedCookie = ""
url = "https://" + serverIP + "/test/login/" + "?refresh=" + Guid()
FLoginWeb.FWebBrowser.Silent = True
FLoginWeb.FWebBrowser.Navigate url
FLoginWeb.Show vbModeless
Do Until InStr(FLoginWeb.FWebBrowser.Document.cookie, "token") > 0: DoEvents: Loop
If FLoginWeb.FWebBrowser.Document.cookie <> "" Then
encodedCookie = FLoginWeb.FWebBrowser.Document.cookie
FLoginWeb.Hide
End If
GetEncodedCookieFromWebBrowser = encodedCookie
End Function
注意
- 由于VBA的WebBrowser是IE内核,而微软官方已经宣布放弃支持IE,因此随着Angular、VUE等版本的升级,可能会出现WebBrowser无法正常加载网页的情况,需要多做测试。
- WebBrowser默认使用的版本是IE7的兼容模式,为了得到更好的体验,可以修改注册表改为支持IE11,方法如下:
Public Sub SetWebBrowserIE11()
Dim fso
Dim RegKey_User_IE As String
Dim oWshell
Dim excelKey As String
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
RegKey_User_IE = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION\excel.exe"
Set oWshell = CreateObject("WScript.Shell")
excelKey = oWshell.RegRead(RegKey_User_IE)
If excelKey = "" Then
oWshell.RegWrite RegKey_User_IE, "11000", "REG_DWORD"
End If
Set oWshell = Nothing
End Sub
边栏推荐
- 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- At the age of 26, he published 18 papers and just proved the prime number conjecture of the last century
- 【深度优先搜索】玩具蛇:迷宫问题
- ARIMA加法季节模型
- TCP/IP协议机制详解
- How can minority majors solve the problem of "growth"?
- 苏涛:对抗样本技术在互联网安全领域的应用
- 零基础学网络:命令行(CLI)调试防火墙实战
- MySQL事务
- Import word document picture VM virtual machine network settings
猜你喜欢

AI 考生挑战高考作文,平均 1 秒生成 1 篇,水平超 75% 考生

面试了一位7年软件测试工作者:成也“7”年,败也“7”年

CNN's performance is even stronger without looking at the whole picture and the parts

The golden band is the fourth largest operator's mace, and farmers will get low-cost 5g services

Qiniu cloud backup website

C listbox usage

研一自然辩证法期末考试试卷2

U.S. restrictions on sharing security vulnerabilities will throw stones at its own feet, and domestic systems will gain development opportunities

Win11 enable multi-user remote desktop simultaneous login

代码实现WordPress编写文章可以调整字体字号
随机推荐
Qiniu cloud backup website
ThreadLocal基础及高级使用
Software configuration item change and baseline change
Uniswapv2 peripheral contract learning (VI) -- exampleoraclesimple sol
Virtual consumption leads new fashion and mutual integration cloud NFT system platform customization development helps enterprises open the "Z" era
Uniswapv2 peripheral contract learning (V) -- exampleflashswap sol
电容电感阻抗模型分析和电源解耦电容选取经验
QUIC会成为互联网传输的颠覆者吗?
HCIA datacom experiment IPv4 addressing and IPv4 routing basic experiment
Uniswapv2 peripheral contract learning (VII) -- exampleslidingwindoworacle sol
C # calculate two time intervals
4427 node sum in tree (greedy)
临界区、事件、互斥量、 信号量--四种控制多线程同步与互斥的方法
发邮件:错排问题的分析
硬件基础——模拟电路
期货外盘开户,还是内盘开户安全??
<测试>基础知识面试考点
Little known beyond relu, it was discovered three years later: the activation functions used by Bert, gpt-2, etc
简单说说路由器和交换机的区别
Solution to the problem that the WordPress address (URL) cannot be opened after modification