I am creating a secure (SSL) public service where the users credentials reside in Active Directory. I want to leverage ServiceStack\'s Authentication and have read over the wiki
I've also hooked up ServiceStack with Integrated Windows Authentication (for a corporate application), and the key was to skip trying to integrate it with ServiceStack's AuthProviders entirely, since the general approach of IWA doesn't deal with credentials in your application code -- it's handled by the web server. What I did was:
Configure the site/application in IIS so that Windows Authentication was the only enabled option. (No Anonymous access allowed.) This means IIS itself will take care of the challenge-response (HTTP 401/200) sequence with unauthenticated users, and handles the authentication part of the process for you.
Implement ServiceStack's IHasRequestFilter
(an HTTP pre-request filter) as an Attribute (e.g., [AdminOnly]). This filter's RequestFilter method fetches the current username from HttpContext (HttpContext.User.Identity.Name
), looks it up from a repository (which could be a SQL database, flat file, etc.), caches results using ServiceStack's ICacheClient
(memory cache, Redis, etc.), and throws a 403 HttpError if unauthorized.
With this done, all that was necessary was to add the attribute to classes or methods where desired (which gets this authentication/authorization into the service pipeline where desired), and register my desired cache provider in my AppHost implementation, e.g.:
container.Register(new MemoryCacheClient() { FlushOnDispose = false });
It works beautifully.