Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticating ASP.NET Web API

Tags:

I've created a new ASP.NET Web API and things are working well. I'm at the point now where I want to secure the API.

I put the [Authorize] attribute above my base controller and it's working properly if I want to make API calls within the ASP.NET application itself.

However, I'm wondering, what's the best practice for an external client that wants to make API calls and get past the authorization? Also, keeping in mind I have custom authentication logic.

How should the client send over credentials? At what point do I process these credentials?

like image 444
Adam Levitt Avatar asked Nov 05 '13 16:11

Adam Levitt


People also ask

How do I authenticate 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 will you implement basic authentication in ASP Net Web API?

In IIS Manager, go to Features View, select Authentication, and enable Basic authentication. In your Web API project, add the [Authorize] attribute for any controller actions that need authentication. A client authenticates itself by setting the Authorization header in the request.

How does authentication and authorization work in Web API?

The ASP.NET Web API Framework provides a built-in authorization filter attribute i.e. AuthorizeAttribute and you can use this built-in filter attribute to checks whether the user is authenticated or not. If not, then it simply returns the HTTP status code 401 Unauthorized, without invoking the controller action method.


2 Answers

How should I send the client credentials?

The default location to send authentication info, is the authorization header. You can use this for basic authentication but also for other types of authentication (JWT, Bearer, etc.).

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

To add, for example, a basic authentication header to your request you could use the following code on your client:

WebRequest request = (HttpWebRequest)WebRequest.Create("https://yoururl");
request.Headers.Add(HttpRequestHeader.Authorization, "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("user:password")));

At what point do I process these credentials?

I would write a DelegatingHandler and use it to resolve your 'principal'. You can then set it to the HttpContext.CurrentPrincipal to have it available wherever you need it within the scope of the request. The DelegatingHandler is called before your controllers as you can see in the image below, which makes it ideal for authentication logic.

enter image description here

I would do the same on the client (write a DelegatingHandler or ActionFilterAttribute) to add the authentication header on a default location. Note that DelegatingHandlers are part of the HTTP pipeline and ActionFilterAttributes belong to the MVC pipeline.

Last but not least I would recommend not to write your own custom authentication logic but stick with one off the default frameworks. This can be as easy as using basic authentication over HTTPS and as complicated as implementing OAuth. But I would stay away from do it yourself solutions.

I did like to also invite you to have a look at this answer I gave to a similair question.

Note: ASP.NET Web Api is REST based, so imho you don't want to keep session information at all.

Edit: For an example on how to implement a delegatinghandler that handle basic authentication see: basic http authentication in asp.net web api using message handlers.

like image 130
Jos Vinke Avatar answered Nov 13 '22 09:11

Jos Vinke


Basically you'll want to send the username and password encrypted over the net to your server application, then you can let your API generate a random session ID and keep it in a list (serverside) and send the ID back to the client. Now each time your client sends something to the server, include the ID he received in the packets and so the server can check it each time.

On client disconnection or fixed timeout you can remove the ID from the server list and ask the client to re-authenticate.

like image 41
Chuk Ultima Avatar answered Nov 13 '22 08:11

Chuk Ultima