Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross platform authentication using ASP.NET Web API

How do I even begin coding authentication using ASP.NET Web API so it is cross-platform to support desktop, mobile and web? I'd read of some methods of doing RESTful authentication, such as using tokens in the header.

Are there any example projects out there that utilizes this method?

Questions:

  1. If not how do I fix the [Authorize] attribute to read the token?
  2. How do I generate this token? I dont think i can use formsauthentication because that uses cookies.
  3. How do I handle the actual authorization, do the client send raw password and username then I generate the token or is there some other way?
  4. How do I handle when my website is using it? I heard this is handled differently than when an app is using it, such as getting the domain and authorizing it.
like image 285
Shawn Mclean Avatar asked Mar 26 '12 23:03

Shawn Mclean


People also ask

Is ASP Net Web API is cross platform?

ASP.NET Core is an open-source, cross-platform framework for building modern, cloud-based web apps on Windows, macOS, or Linux.

How do I provide authentication in Web API?

Web API assumes that authentication happens in the host. For web-hosting, the host is IIS, which uses HTTP modules for authentication. You can configure your project to use any of the authentication modules built in to IIS or ASP.NET, or write your own HTTP module to perform custom authentication.

How do I enable cross origin in Web API?

You can enable CORS per action, per controller, or globally for all Web API controllers in your application. To enable CORS for a single action, set the [EnableCors] attribute on the action method.


2 Answers

I think tokens would be a solid way to go. Forms authentication is based on cookies for the web. Not the most idea situation for all non browser clients though.

What I'd suggest is creating a custom AuthorizationFilterAttribute and overriding the OnAuthorization method. In that method, you could check for the existence of a token that you've issued to the client after they've supplied valid credentials. You can use this attribute on any method or controller you want validated. Here's a sample you might reference

 public class AuthorizeTokenAttribute : AuthorizationFilterAttribute  {           public override void OnAuthorization(HttpActionContext actionContext)     {         if (actionContext != null)         {                                 if (!AuthorizeRequest(actionContext.ControllerContext.Request))                 {                     actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized) { RequestMessage = actionContext.ControllerContext.Request };                  }                 return;         }     }      private bool AuthorizeRequest(System.Net.Http.HttpRequestMessage request)     {         bool authorized = false;         if (request.Headers.Contains(Constants.TOKEN_HEADER))         {                            var tokenValue = request.Headers.GetValues("TOKEN_HEADER");             if (tokenValue.Count() == 1) {                 var value = tokenValue.FirstOrDefault();                               //Token validation logic here                //set authorized variable accordingly             }                         }         return authorized;     } } 

TOKEN_HEADER is just a string representing an HTTP header that the client should pass back for authenticated requests.

So let's walk through it

  1. Client requests secure data
  2. Client is not authorized, return a response with an Unauthorized status code
  3. Client sends credentials to authenticate, which should be secured via HTTPS
  4. Once validated, client receives a token via an HTTP header, or whatever works for you
  5. Client tries requesting secure data again, this time attached the token to the request
  6. The AuthorizeTokenAttribute will validate the token and allow the action to execute.

Also, check this post by John Petersen. Making your ASP.NET Web API’s secure

like image 194
cecilphillip Avatar answered Oct 04 '22 11:10

cecilphillip


There are lots of ways to authenticate users for a REST service. Using tokens is possible but just using Basic Authentication is even simpler and about as standard and cross platform as you can go.

Don't confuse authorization with authentication. The [Authorize] attribute is all about authorization but only after a user has been authenticated using some other mechanism. Authorization is completely useless without doing proper authentication first.

The best resource to check is Dominick Baier who is an expert on the subject.

like image 32
Maurice Avatar answered Oct 04 '22 13:10

Maurice