Julz
Julz

Reputation: 65

REST WCF 4 Service with Custom Basic Authentication over SSL in IIS

I'm looking for a complete step-by-step guide or a sample project about implementing a RESTful Service using .NET 4.0 using Custom Basic Authentication over HTTPS hosted in IIS.

I've been googling about it for 3 days and I could only find either an implementation with WCF 3.5 which is very different, or without Custom Basic Authentication, or without SSL.

Basically I implemented my REST service on WCF 4, and added SSL, but I can't use a custom authentication using my custom users database.

Any references would be really appreciated.

Upvotes: 4

Views: 3178

Answers (3)

Robert Ellison
Robert Ellison

Reputation: 164

I wrestled with this for a while and ended up just implementing basic auth in my service. Check WebOperationContext.Current.IncomingRequest.Headers for an 'Authorization' header. If it's missing or the credentials don't match set the challenge header and return a 401 status:

WebOperationContext.Current.OutgoingResponse.Headers.Add("WWW-Authenticate: Basic realm=\"myrealm\""); 
throw new WebFaultException<string>("Username and password needed", HttpStatus.Unauthorized);

That's enough to trigger a browser to prompt the user for credentials. See https://www.rfc-editor.org/rfc/rfc2617 for more on basic auth, http://ithoughthecamewithyou.com/post/Basic-HTTP-auth-for-an-IIS-hosted-WCF-4-RESTful-service.aspx for more on this frustrating missing capability.

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364269

It is possible with custom HTTP module allowing basic authentication against custom credential store. Built-in module in IIS supports only windows accounts.

Upvotes: 0

Phil Degenhardt
Phil Degenhardt

Reputation: 7264

It's not currently possible using the available WCF extension points.

Upvotes: 1

Related Questions