Forms Based Authentication in VBScript

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



HI I'm trying to use Winhttp to authenticate with an exchange server (Forms
based authentication enabled) and then use the recieved cookies in the next
server request.... But all I'm getting is Login Time Out which means I'm not
able to set the cookies correctly in the next request exchange server....
Could you guys please help me with the code I pasted below.... I need it
urgently...

I was able to do it successfully in C# but I need it in VBScript

Here is the code I'm using...


strUrl = strProtocol & "://" & strExchangeServerName &
"/exchweb/bin/auth/owaauth.dll"
Set oHttpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oHttpRequest.open "POST", strUrl, false
Dim strQuery : strQuery = "destination=" & exchangeservername &
"&flags=0&username="&username & "&password=" & escape(password) &
"&SubmitCreds=Log+On&forcedownlevel=0&trusted=true"
oHttpRequest.setRequestHeader "Content-Type",
"application/x-www-form-urlencoded"
const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
oHttpRequest.SetCredentials strDomain + "\" + strUser , strPwd,
HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
Const WinHttpRequestOption_SslErrorIgnoreFlags = 4
Const WinHttpRequestOption_EnableRedirects= 6
Const WINHTTP_OPTION_SECURITY_FLAGS = 13056
oHttpRequest.Option(WinHttpRequestOption_SslErrorIgnoreFlags) =
WINHTTP_OPTION_SECURITY_FLAGS
oHttpRequest.Option(WinHttpRequestOption_EnableRedirects) = "False"
oHttpRequest.Send(strQuery)
str = OHttpRequest.GetAllResponseHeaders
Set regex = new RegExp
regex.pattern = "^Set-Cookie: ([^\s=]+)=([^\s;]+);"
regEx.IgnoreCase = True
regEx.Global = True
regEx.MultiLine = True
MsgBox str
Set oHttpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oHttpRequest.Open "PROPFIND", strProtocol & "://" & strExchangeServerName &
"/exchange/" & strMailBox & "/"
strQuery = "<?xml version='1.0'?><a:propfind xmlns:a='DAV:'
xmlns:mail='urn:schemas:httpmail:'><a:prop><mail:inbox
/></a:prop></a:propfind>"
oHttpRequest.SetRequestHeader "Depth", "0"
oHttpRequest.SetRequestHeader "Content-Type","text/xml"
dim toSet
Set CurrentMatches = regex.Execute (str)
count = CurrentMatches.Count
Do while count > 0
Set CurrentMatch = CurrentMatches(CurrentMatches.Count - count)
' MsgBox CurrentMatch
If CurrentMatch.SubMatches.Count >= 1 Then
innerCount = CurrentMatch.SubMatches.Count
if innerCount mod 2 = 0 Then
Do while innercount > 0
if innerCount mod 2 = 0 Then
cookieName = CurrentMatch.Submatches(CurrentMatch.SubMatches.Count -
innerCount)
else
cookieValue = CurrentMatch.Submatches(CurrentMatch.SubMatches.Count -
innerCount)
end if
innerCount = innerCount - 1
Loop
if toSet = "" then
toSet = cookieName & "=" & cookieValue
else
toSet = toSet & "; " & cookieName & "=" & cookieValue
end if
End If
End If
count = count - 1
Loop
msgBox toSet
oHttpRequest.SetRequestHeader "Cookie",toSet
oHttpRequest.Option(WinHttpRequestOption_EnableRedirects) = "False"
Set regEx = Nothing
oHttpRequest.Send(strQuery)
MsgBox oHttpRequest.ResponseText

It shows Login Timeout in the responsetext property

Thanking you
Sandeep
.



Relevant Pages

  • Re: Forms Based Authentication in VBScript
    ... able to set the cookies correctly in the next request exchange server.... ... Set CurrentMatches = regex.Execute ... ' MsgBox CurrentMatch ... innerCount = CurrentMatch.SubMatches.Count ...
    (microsoft.public.exchange.development)
  • Re: Forms Based Authentication in VBScript
    ... I have a VBScript example on my website: http://www.infinitec.de/articles/exchange/webdavwithfba.aspx ... able to set the cookies correctly in the next request exchange server.... ... ' MsgBox CurrentMatch ... innerCount = CurrentMatch.SubMatches.Count ...
    (microsoft.public.exchange.development)