Forms Authentication, Web Service, and AJAX




I'm using a plain vanilla AJAX (not ASP.NET Ajax) in my web application:

// The name of the Web Service method: GetData
// Parameters: CatID, pageSize
var soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:xsi=\"\"
xmlns:soap=\"\">" +
" <soap:Body>" +
" <GetData xmlns=\"\">" +
" <catId>2</catId>" +
" <pageSize>10</pageSize>" +
" </GetData>" +
" </soap:Body>" +

// WEB_SERVICE_URL - a URL to my web service
// WEB_SERVICE_HOST - host name
req.Open("POST", WEB_SERVICE_URL, false);
req.setRequestHeader("Host", WEB_SERVICE_HOST);
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("Content-Length", soap.length);

I'm using Forms Authentication in my applicatio but I found that passing the
authentication cookie to the Web Service does not work as expected.

// ASP.NET Authentication code
if (FormsAuthentication.Authenticate("admin", "pswd"))
// Does not matter if the SetAuthCookie is called.
// The web service won't be authenticated anyway.
//FormsAuthentication.SetAuthCookie("admin", false);

// RedirectFromLoginPage calls SetAuthCookie internally.
FormsAuthentication.RedirectFromLoginPage("admin", false);

I tried to use several methods to pass and access the auth. cookie:
- HttpContext.Current.User.Identity.IsAuthenticated (from the web service)
- using the session state with [WebMethod(EnableSession=true)]
- Creating and passing the GenericPrincipal explicitly.
- Creating cookies explicitly and attaching them to the request.

None of the methods work. IsAuthenticated is always false, and
User.Identity.Name is empty.
Oddly enough the session state in the web service is also wiped out. It
seems like the AJAX call to the web service creates a new session! No wonder
there is no auth. cookie attached.

How to pass the auth. cookie to the web service using the AJAX call?

Any help would be greatly appreciated,



Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question