In the past I've used the Msxml2.ServerXMLHTTP
to make a server-side call that switches the impersonation context using the HTTP Authorization Headers. This allows me to create a file using a different context to what the website is running in.
Sub ExecuteContext(url, data, user, password)
Dim http
Set http = Server.CreateObject("Msxml2.ServerXMLHTTP")
Response.CharSet = "utf-8"
Call http.open("POST", url, False, user, password)
'Called using Basic Authentication (not as secure as Windows Authenticated by should be adequate)
Call http.setRequestHeader("Authorization", "Basic " & Base64Encode(user & ":" & password))
Call http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
Call http.send(data)
End Sub
'Supporting functions to do the Base64 encoded Authorization header.
Function Base64Encode(inData)
'ripped from:
'http://www.pstruh.cz/tips/detpg_Base64Encode.htm
'rfc1521
'2001 Antonin Foller, PSTRUH Software, http://pstruh.cz
Const Base64 = _
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim sOut, I
'For each group of 3 bytes
For I = 1 To Len(inData) Step 3
Dim nGroup, pOut
'Create one long from this 3 bytes.
nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
&H100 * MyASC(Mid(inData, I + 1, 1)) + _
MyASC(Mid(inData, I + 2, 1))
'Oct splits the long To 8 groups with 3 bits
nGroup = Oct(nGroup)
'Add leading zeros
nGroup = String(8 - Len(nGroup), "0") & nGroup
'Convert To base64
pOut = Mid(Base64, CLng("&o" & Mid(nGroup, 1, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 3, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 5, 2)) + 1, 1) + _
Mid(Base64, CLng("&o" & Mid(nGroup, 7, 2)) + 1, 1)
'Add the part To OutPut string
sOut = sOut + pOut
Next
Select Case Len(inData) Mod 3
Case 1: '8 bit final
sOut = Left(sOut, Len(sOut) - 2) + "=="
Case 2: '16 bit final
sOut = Left(sOut, Len(sOut) - 1) + "="
End Select
Base64Encode = sOut
End Function
Function MyASC(OneChar)
If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
End Function
This approach is extremely flexible, in my implementation I use to it to change the context in the same page by POSTing to itself but with different data
.