Mega Code Archive

 
Categories / Delphi / Forms
 

Authenticate the Inbox in Exchange Server 2003 with forms based authentication

Title: authenticate the Inbox in Exchange Server 2003 with forms-based authentication Question: How to authenticate the Inbox in Exchange Server 2003 with forms-based authentication? Answer: This is the translated code from VB.net to Delphi.NET for the original article http://support.microsoft.com/?kbid=891748 Uses System, System.Net, System.IO, System.Xml, System.Text.RegularExpressions; function TwebForm1.AuthenticateSecureOWA(StrServerName: string; Credential: NetworkCredential): string; var strCADATACookie, strSessionIDcookie, strCookie, strPostFields, sHTML: string; WebReq: HttpWebRequest; WebResp: HttpWebResponse; PostData: array of Byte; sStream, TmpStream: Stream; AuthURL: System.Uri; tmpStreamRead: StreamReader; begin AuthUrl := System.uri.Create('https://' + StrServerName + '/exchweb/bin/auth/owaauth.dll'); WebReq := WebRequest.CreateDefault(AuthUrl) as HttpWebrequest; CookieJar := CookieContainer.Create; WebReq.CookieContainer := CookieJar; strPostFields := 'destination=https%3A%2F%2F' + strServerName + '%2Fexchange%2F' + Credential.UserName + '%2F&username=' + Credential.Domain + '%5C' + Credential.UserName + '&password=' + Credential.Password + '&SubmitCreds=Log+On' + 'forcedownlevel=0' + '&trusted=0'; WebReq.KeepAlive := True; WebReq.AllowWriteStreamBuffering := True; WebReq.Method := 'POST'; PostData := System.Text.Encoding.ASCII.GetBytes(strPostFields); WebReq.ContentLength := Length(PostData); try TmpStream := WebReq.GetRequestStream; TmpStream.Write(PostData, 0, Length(PostData)); TmpStream.Close; except on E: Exception do Response.Write(E.Message); end; try WebResp := WebReq.GetResponse as HttpWebresponse; except on E: Exception do Response.Write(E.Message); end; if (WebResp.StatusCode = System.Net.HttpStatusCode.OK) then begin tmpStreamRead := StreamReader.Create(WebResp.GetResponseStream); sHTML := tmpStreamRead.ReadToEnd; tmpStreamRead.Close; sStream := WebReq.GetRequestStream; WebResp.Close; Strcookie := CookieJar.GetCookieHeader(AuthUrl).ToString; strCADAtaCookie := Regex.Replace(strcookie, '(.*)cadata=''(.*)''(.*)', '$2'); strSessionIDCookie := Regex.Replace(strCookie, '(.*)sessionid=(.*)(,|;)(.*)', '$2'); Result := 'sessionid=' + strSessionIDCookie + ';' + 'cadate=' + strcadataCookie; end; end; procedure TWebForm1.Button1_Click(sender: System.object; e: System.EventArgs); var strServerNAme: string; strCookie: string; Credential: NetworkCredential; begin strServerName := 'c-rhq-owa1'; Credential := NetworkCredential.Create('UserName', 'Password', 'Domain'); strCookie := AuthenticateSecureOWA(strServerName, Credential); end;