First of all, I believe that never come up with your own authentication mechanism.
To answer your current problems:
1 As a rule, you always want to protect your Api using authentication, as this is the place where you access your data. Your client (MVC App / Smartphone) must log in to access your Api.
2 and 3 Since you are using REST Api, I would suggest keeping your Apache stateless, in other words, not storing session information. Just provide the role details that you need in your token. You can use, for example, JNON Web Token .
4 I would always use the authorization header to send authorization data. In the DelegatingHandler case (note the difference in MessageHandler MVC, DelegatingHander HTTP), you can just get the header.
protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { var authorizationHeader = request.Headers.Authorization; // Your authorization logic. return base.SendAsync(request, cancellationToken); }
For details on how to enable the authorization header in an ajax call, see below: How to use Basic Auth with jQuery and AJAX?
Additional Information:
If I were you, I would also look at the Thinktecture Identity Server: https://github.com/thinktecture/Thinktecture.IdentityServer.v2
And maybe this REST service authentication answer will also help you: REST Service Authentication
Jos vinke
source share