How to use Servicestack Authentication with Active Directory/Windows Authentication?

后端 未结 2 1528
感情败类
感情败类 2021-02-01 17:49

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

2条回答
  •  野性不改
    2021-02-01 18:16

    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:

    1. 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.

    2. 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.

提交回复
热议问题