Sharing authentication methods across API and web app

Posted by Snixtor on Programmers See other posts from Programmers or by Snixtor
Published on 2012-11-19T00:32:08Z Indexed on 2012/11/19 5:25 UTC
Read the original article Hit count: 432

Filed under:
|
|

I'm wanting to share an authentication implementation across a web application, and web API. The web application will be ASP.NET (mostly MVC 4), the API will be mostly ASP.NET WEB API, though I anticipate it will also have a few custom modules or handlers.

I want to:

  1. Share as much authentication implementation between the app and API as possible.
  2. Have the web application behave like forms authentication (attractive log-in page, logout option, redirect to / from login page when a request requires authentication / authorisation).
  3. Have API callers use something closer to standard HTTP (401 - Unauthorized, not 302 - Redirect).
  4. Provide client and server side logout mechanisms that don't require a change of password (so HTTP basic is out, since clients typically cache their credentials).

The way I'm thinking of implementing this is using plain old ASP.NET forms authentication for the web application, and pushing another module into the stack (much like MADAM - Mixed Authentication Disposition ASP.NET Module). This module will look for some HTTP header (implementation specific) which indicates "caller is API".

If the header "caller is API" is set, then the service will respond differently than standard ASP.NET forms authentication, it will:

  1. 401 instead of 302 on a request lacking authentication.
  2. Look for username + pass in a custom "Login" HTTP header, and return a FormsAuthentication ticket in a custom "FormsAuth" header.
  3. Look for FormsAuthentication ticket in a custom "FormsAuth" header.

My question(s) are:

  1. Is there a framework for ASP.NET that already covers this scenario?
  2. Are there any glaring holes in this proposed implementation? My primary fear is a security risk that I can't see, but I'm similarly concerned that there may be something about such an implementation that will make it overly restrictive or clumsy to work with.

© Programmers or respective owner

Related posts about ASP.NET

Related posts about api