Monday, February 04, 2008

How to Get ADO.NET Data Services (Astoria) Working With Forms Authentication

UPDATE: Since I wrote this post, I've given a presentation on Astoria, with an updated Forms Authentication example. This post has a link to the slides and demo code.


As I was playing around with ADO.NET Data Services, I was having some trouble getting the services to work in my forms authentication enabled site. every time I tried going to the service, I kept getting redirected to the login page. Even if I was already logged in.

First thing I tried was using the location node in the web.config to exclude my service. That didn't work. Then I tried changing my authorization node to allow all users. That still didn't work. The only thing I could get to work was to remove the forms authentication node all together.

Obviously, removing security from my site wasn't an option, so I started to poke around in Reflector a bit. Still no dice. I had no luck finding anything that could possibly be checking forms authentication and trying to redirect to the login page.

So, one thing I thought I'd try is adding some information into the web.config serviceModel node. I had been wondering how that node interacted with Astoria, because in my experience with WCF, the service configuration nodes are extremely important. So, I thought I'd toss in some really basic info to the config and see if that did it. So, I put the following in my web.config.

<system.serviceModel>
<services>
<service name="MyDataService">
<endpoint address="" binding="webHttpBinding" contract="Microsoft.Data.Web.IRequestHandler" />
</service>
</services>
</system.serviceModel>

And as you can guess (or not), that worked like a charm. Hopefully someone from the ADO.NET team has a look see to see why this is the case.

6 comments:

Anonymous said...

still trying to have Form Authentication but with no luck at all.
i tryed your workaround but still redirected to login page.
is there anything missing,.

thanks

Tony Pujals said...

The fix worked for us. Thanks a lot. The anonymous poster above having trouble is probably copying the fix literally, instead of providing the correct service name attribute...

Danny Crowell said...

Jim, this is excellent. I have been searching all over the web for information on how to use forms authentication with ADO.net Data Services. Do you have any sample code on how to call the secured data service from an ASP.net client?

Thanks, Dan

Jim Fiorato said...

Hey Dan

You can see some samples in the code samples from the Codeapalooza presentation I gave in September.

Anonymous said...

Can you share a demo projec for this ?

Jim Fiorato said...

Since I wrote this post, I've given a presentation on Astoria, with an updated Forms Authentication example. This post has a link to the slides and demo code: http://www.writebetterbits.com/2008/09/codeapalooza-slides-and-code-samples.html