How to authenticate the Inbox in Exchange Server 2003 with forms-based authentication enabled by using Visual Basic .NET

This article has been archived. It is offered "as is" and will no longer be updated.
INTRODUCTION
This article describes how to authenticate the Inbox in Microsoft Exchange Server 2003 with forms-based authentication enabled by using Microsoft Visual Basic .NET.
MORE INFORMATION
To access the Inbox in Exchange Server 2003, you must set up WebDAV access to Exchange Server 2003 in your Web application. You must also authenticate the Inbox in Exchange Server 2003 to the Exchange Server 2003 server that is enabled with forms-based authentication. The following Visual Basic .NET code sample and Visual Basic .NET function accomplish both of these tasks.

WebDAV access

To set up WebDAV access to Exchange Server 2003 in your Web application, use the following Visual Basic .NET code sample as a reference.
Dim strServerName as String = "Server Name" 	' TODO: Change to your environmentDim strDomain as String = "Domain Name"		' TODO: Change to your environmentDim strUserID as String = "Username"		' TODO: Change to your environmentDim strPassword as String = "Password"		' TODO: Change to your environment' Create our destination URL.Dim strURL As String = "https://" & strServerName & "/exchange/" & strUserName & "/inbox/test.eml"Dim strReusableCookies As String' Create our Web request object.GETRequest = CType(WebRequest.Create(New System.Uri(strURL & strApptItem)), HttpWebRequest)strReusableCookies = AuthenticateSecureOWA(strServerName, strDomain, strUserID, strPassword)' Add the cookie set that is obtained after OWA authentication to our request header.PROPPATCHRequest.Headers.Add("Cookie", strReusableCookies)PROPPATCHRequest.ContentType = "text/xml"PROPPATCHRequest.KeepAlive = TruePROPPATCHRequest.AllowAutoRedirect = False' Specify the PROPPATCH method.PROPPATCHRequest.Method = "GET"' Enter your WebDAV-related code here.
Note In the Visual Basic .NET code sample that was just mentioned, the strReusableCookies string variable is the authentication cookie that is returned from the AuthenticateSecureOWA function call. If the authentication cookie times out, call the AuthenticateSecureOWA function again to receive a new authentication cookie.

Another way to work around this problem is to put the WebDAV request in a try/catch block. The try/catch block will catch the authentication cookie time-out error. When the authentication cookie time-out error occurs, you can re-authenticate the Inbox in Exchange Server 2003 to the Exchange Server 2003 server that is enabled with forms-based authentication.

Authentication function

To authenticate to Exchange Server 2003 with forms-based authentication enabled from your Web application, use the following Visual Basic .NET function:
Imports SystemImports System.NetImports System.IOImports System.XmlImports System.Text.RegularExpressions' Code to call the Authentication:    Private CookieJar As CookieContainer    Private strCookies As String' Authenticate to OWA. Assign the returned cookies to a string.        Dim strReusableCookies As String        strReusableCookies = AuthenticateSecureOWA(strServerName, strDomain, strUserID, strPassword)'Implementation of the Authentication to the Exchange Server 2003 server that is enabled with forms-based authentication    Private Function AuthenticateSecureOWA(ByVal strServerName As String, ByVal strDomain As String, ByVal strUserName As String, ByVal strPassword As String) As String    Dim AuthURL As System.Uri        Try            ' Construct our destination URI.                AuthURL = New System.Uri("https://" + strServerName + "/exchweb/bin/auth/owaauth.dll")        Catch ex As Exception.                MsgBox("Error occurred while you are creating the URI for OWA authentication!" + vbCrLf + vbCrLf + ex.Message)                Return "Error"        End Try    Dim WebReq As HttpWebRequest    CookieJar = New CookieContainer' Create our request object for the constructed URI.    WebReq = CType(WebRequest.Create(AuthURL), HttpWebRequest)    WebReq.CookieContainer = CookieJar' Create our post data string that is required by OWA (owaauth.dll).    Dim strPostFields As String = "destination=https%3A%2F%2F" & strServerName & "%2Fexchange%2F" + strUserName + "%2F&username=" + strDomain + "%5C" + strUserName + "&password=" + strPassword + "&SubmitCreds=Log+On&forcedownlevel=0&trusted=0"    WebReq.KeepAlive = True    WebReq.AllowAutoRedirect = False    WebReq.Method = "POST"' Store the post data into a byte array.    Dim PostData() As Byte = System.Text.Encoding.ASCII.GetBytes(strPostFields)' Set the content length.    WebReq.ContentLength = PostData.Length    Dim tmpStream As Stream        Try            ' Create a request stream. Write the post data to the stream.                tmpStream = WebReq.GetRequestStream()                tmpStream.Write(PostData, 0, PostData.Length)                tmpStream.Close()        Catch ex As Exception.                MsgBox("Error occurred while trying OWA authentication!" + vbCrLf + vbCrLf + ex.Message)                Return "Error"        End Try' Get the response from the request.    Dim WebResp As HttpWebResponse = WebReq.GetResponse()' Create a stream to capture the response data    Dim tmpStreamRead As New StreamReader(WebResp.GetResponseStream())' Write returned data to a string.    Dim strResponseData As String = tmpStreamRead.ReadToEnd()    tmpStreamRead.Close()' Close the response object.    WebResp.Close()' Get our returned cookie set.    strCookies = CookieJar.GetCookieHeader(AuthURL).ToString()' Filter for our cadata and session ID cookies.    Dim strCADataCookie As String = Regex.Replace(strCookies, "(.*)cadata=""(.*)""(.*)", "$2")    Dim strSessionIDCookie As String = Regex.Replace(strCookies, "(.*)sessionid=(.*)(,|;)(.*)", "$2")' Create and return the cookie set for performing subsequent Web requests.    strCookies = "sessionid=" + strSessionIDCookie + "; " + "cadata=" + strCADataCookie    Return strCookies    End Function
REFERENCES
For more information about Outlook Web Access features in Exchange Server 2003, click the following article number to view the article in the Microsoft Knowledge Base:
830827 How to manage Outlook Web Access features in Exchange Server 2003
OWA FAB
Properties

Article ID: 891748 - Last Review: 01/17/2015 09:43:14 - Revision: 1.4

  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • kbnosurvey kbarchive kbexchdev kbprogramming kbhowto kbinfo KB891748
Feedback