问题
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?
回答1:
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.
I would do the same on the client (write a DelegatingHandler
or ActionFilterAttribute
) to add the authentication header on a default location. Note that DelegatingHandler
s are part of the HTTP pipeline and ActionFilterAttribute
s 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.
回答2:
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.
来源:https://stackoverflow.com/questions/19793845/authenticating-asp-net-web-api