If you want a truly secure authentication option, then, for example, OAuth is the way to go. This blog post provides a fairly thorough example using the now deprecated WCF web API, but many of the code is safe. Or at least use basic HTTP authentication, as shown in this post. As Aliostad notes, make sure you use HTTPS if you go along the basic authentication route so that the token remains secure.
If you decide that you want to collapse your own (which will almost always be much less secure than any of the above options), below is an example of the code you will need for an AuthorizationHanlder if you are sending an HTTP header route. Keep in mind that a good chance of how UserPrinicipal is handled in web API classes can change, so this code is only good for the first preview. You will need to connect to the AuthorizationHandler as follows:
GlobalConfiguration.Configuration.MessageHandlers.Add(new AuthenticationHandler());
Code for header token:
public class AuthenticationHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { var requestAuthTokenList = GetRequestAuthTokens(request); if (ValidAuthorization(requestAuthTokenList)) {
Sixto Saez May 03 '12 at 15:55 2012-05-03 15:55
source share