Re: Close OWA Connection
- From: "Glen Scales [MVP]" <gscales@xxxxxxxxxxxxxxxxxxx>
- Date: Thu, 4 May 2006 09:58:50 +1000
If your using Forms Based Authentication then yes you have to send cookies
with any request you send to the server its just the same as any other
requestion you make to a exchange server.
You can adjust the timeout of cookies with certain registry keys but I would
consider it a better practice to logon and logoff when you don't need the
connection open unless you doing large batch process's. If you want to keep
the connection open you going to need to write some cookie lifecycle logic
that monitors the age of the cookie and then logoff and logon to get a new
one when required. Or create a separate virtual directory for your
application to run against that doesn't have FBA enabled.
The only documentation i've ever seen for OWA command is in the OWA
customiation whitepaper for Exchange 2000
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=6532E454-073E-4974-A800-1490A7CB358F
..Unfortunatly this hasn't been updated for Exchange 2003 to include the new
commands that where added. You might want to look at using something like
Etheral or ieHTTPHeaders to mointer the traffice that goes between a server
when you use OWA and then you can examine what the format of the request are
supposed to look like.
Cheers
Glen
"NgelW" <NgelW@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:21FD60A1-0A2B-4DED-BE0A-8F76B3E9CB14@xxxxxxxxxxxxxxxx
Hi,
After connecting to exchange, I collect the cookies as such:
----- code snippet -----
try
{
//Get server portion of uri and append Exchange auth dll
server = uri.Substring(0, uri.IndexOf("/", 8)) +
"/exchweb/bin/auth/owaauth.dll";
//Build request
request = GetRequestObject(server, "POST");
request.CookieContainer = new CookieContainer();
request.ContentType = "application/x-www-form-urlencoded";
//Prepare body of request
body =
Encoding.UTF8.GetBytes(string.Format("destination={0}&username={1}\\{2}&password={3}",
uri, credential.Domain, credential.UserName, credential.Password));
request.ContentLength = body.Length;
//Send request to server
stream = request.GetRequestStream();
stream.Write(body, 0, body.Length);
stream.Close();
//Get response
response = (HttpWebResponse)request.GetResponse();
//Check if login successful
if (response.Cookies.Count < 2) throw
new Exception("Failed to login to OWA. Be sure your domain and password
are
correct.");
//Build OverLoaded Cookie Collection
CookieCollection responseCookies = new CookieCollection();
//Add cookies to this collection
responseCookies.Add(response.Cookies[0]);
responseCookies.Add(response.Cookies[1]);
//Return the cookie collection
return responseCookies;
}
----- code snippet -----
I then add these cookies into any further requests, which seems to work
fine, for a period of time. Then I start getting Timeouts. I assume this
is
because my exchange session as timed-out. To get around this, I wanted to
close my exchange session (i.e logoff) after every connection I make. I
tried the code you provided, but no luck. Would I not have to include the
cookies in the HTTP request containing the ?cmd=logoff command, or is the
credential set enough info for exchange? ((that wouldn't seem very
secure))
Is this the appropriate way to maintain a connection to exchange - I would
either like the timeout period on the exchange session renewed upon every
action (and a re-login when it timesout with inactivity), or preferably,
the
session to be opened and closed on every request (hence never have a
timeout
problem).
Also, is there a reference to what commands like \?Cmd=logoff are
availabkle
with exchange server?? i have the Exchange 2003 SDK installed - but there
doesn't seem to be a whole lot in it....
I probably should mention that this is for a sharepoint (wss) web part.
Many thanks,
Nigel W
"Glen Scales [MVP]" wrote:
That error sounds like your trying to send some sort of body with you get
requestion which you shouldn't be doing the following code snippet seems
to
work okay for me
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
namespace ExchangeSDK.Snippets.CSharp
{
class GettingItemPropertyValuesWebDAV
{
[STAThread]
static void Main(string[] args)
{
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strSrcURI = "http://server/exchange/mailbox/?Cmd=logoff";
string strUserName = "user";
string strPassword = "password";
string strDomain = "domain";
System.IO.Stream ResponseStream = null;
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add( new System.Uri(strSrcURI),
"Basic",
new System.Net.NetworkCredential(strUserName, strPassword,
strDomain)
);
Request =
(System.Net.HttpWebRequest)HttpWebRequest.Create(strSrcURI);
Request.Credentials = MyCredentialCache;
Request.Method = "GET";
Request.ContentType = "text/xml";
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
StreamReader objReader = new StreamReader(ResponseStream);
string sLine = "";
int i = 0;
while (sLine!=null)
{
i++;
sLine = objReader.ReadLine();
if (sLine!=null)
Console.WriteLine("{0}:{1}",i,sLine);
}
Console.ReadLine();
ResponseStream.Close();
Response.Close();
}
}
}
Cheers
Glen
"NgelW" <NgelW@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:B4B7BC33-B4DD-43DA-8C69-1BDE070517AA@xxxxxxxxxxxxxxxx
Thanks - I have tried to implement this as a GET call (hacked from a
function
performing a POST call), but it returns a "Cannot send a content-body
with
this verb-type" error <see code snippet below>
Is there (yet again) something obvious that I am doing wrong? Also, it
suprises me a little that I don't need to use the cookies gained when I
established my connection to OWA to terminate the connection?!?
----- code snippet -----
public void BreakAuthentication(string uri)
{
//Paramters
HttpWebRequest request;
HttpWebResponse response;
byte[] body;
Stream stream;
string uriToUse;
//Update URI
uriToUse = uri.Substring(0, uri.IndexOf("/", 8)) + "/?Cmd=logoff";
//Build request
request = GetRequestObject(uriToUse, "GET");
//This is probably the problem?!? What sort of Content-Type should I
//use?
request.ContentType = "application/x-www-form-urlencoded";
//Send request to server
stream = request.GetRequestStream();
stream.Close();
//Get response
response = (HttpWebResponse)request.GetResponse();
}
---- end code snippet ----
"Glen Scales [MVP]" wrote:
Are you using Exchange 2003 is so you could just make a call to the
OWA
Logoff command eg
GET /exchange/user/?Cmd=logoff
cheers
Glen
"NgelW" <NgelW@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:77572BDF-93BC-4C03-B904-264774EEE3DE@xxxxxxxxxxxxxxxx
Hi,
In a c# app, I have established a connection to OWA, and done what I
need
to
do on the exchange server, and would now like to close that
connection
rather
then allowing it to time-out. Can anyone explain how this can be
done?
Many thanks,
Nigel
.
- References:
- Re: Close OWA Connection
- From: NgelW
- Re: Close OWA Connection
- Prev by Date: Re: How to find deleted Contacts and appointments & task folder properties
- Next by Date: Attachment Overload Solution
- Previous by thread: Re: Close OWA Connection
- Next by thread: Attachment Overload Solution
- Index(es):
Relevant Pages
|