Max*_*ime 4 asp.net-mvc vba windows-authentication
我开发了一个 MVC 应用程序,为了解决这个问题,它只有一个控制器:
Public Function GetValue()
Return User.Identity.Name
End Function
Run Code Online (Sandbox Code Playgroud)
该应用程序要在 Intranet 网络上使用,因此,我将其设置为“Windows 身份验证”
目的是通过 VBA 查询该应用程序。
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
With objHTTP
.Open "GET", URL, False
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.setRequestHeader "Content-type", "application/json"
.setRequestHeader "data-type", "json"
.send
.WaitForResponse
sResult = .ResponseText
End With
Debug.Print (sResult)
Run Code Online (Sandbox Code Playgroud)
如果我在本地运行该应用程序(即在运行 Excel 的计算机上进行调试),它可以通过 Chrome 运行(访问 localhost:xxxxx/api/name 返回带有我的 ActiveDirectory 用户名的 xml 文件)。
VBA 例程也运行良好,输出窗口显示我在 Chrome 中获得的 XML。
现在,如果我将项目发布到 IIS 服务器,它仍然可以通过 Chrome 运行(访问 myserver/api/name 会返回带有我的 ActiveDirectory 用户名的 xml 文件)。但是,当我运行 VBA 模块时,它返回错误 401:
Error:401 - Unauthorized: Access is denied due to invalid credentials.
Run Code Online (Sandbox Code Playgroud)
它在浏览器中工作的事实让我相信服务器端配置是好的,并且我需要在 VBA 中调整一些东西。
我必须承认我现在有点无能为力...谢谢您给我的任何线索:)
感谢@SWa 评论,我通过对函数的一个小调整解决了这个问题:切换到 WinHttpRequest 并使用setAutoLogonPolicy 0
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
With objHTTP
.Open "GET", URL, False
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
.setRequestHeader "Content-type", "application/json"
.setRequestHeader "data-type", "json"
.setAutoLogonPolicy 0
.send
.WaitForResponse
sResult = .ResponseText
End With
Debug.Print (sResult)
Run Code Online (Sandbox Code Playgroud)