简体   繁体   English

验证ASP.NET Web API

[英]Authenticating ASP.NET Web API

I've created a new ASP.NET Web API and things are working well. 我已经创建了一个新的ASP.NET Web API,并且运行良好。 I'm at the point now where I want to secure the API. 我现在正处于我想保护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. 我将[Authorize]属性放在我的基本控制器上面,如果我想在ASP.NET应用程序本身内进行API调用,它就能正常工作。

However, I'm wondering, what's the best practice for an external client that wants to make API calls and get past the authorization? 但是,我想知道,对于想要进行API调用并通过授权的外部客户端,最佳做法是什么? Also, keeping in mind I have custom authentication logic. 另外,请记住我有自定义身份验证逻辑。

How should the client send over credentials? 客户端应该如何发送凭据? At what point do I process these credentials? 我在什么时候处理这​​些凭证?

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.). 您可以将其用于基本身份验证,也可以用于其他类型的身份验证(JWT,承载等)。

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'. 我会写一个DelegatingHandler并用它来解决你的“委托人”。 You can then set it to the HttpContext.CurrentPrincipal to have it available wherever you need it within the scope of the request. 然后,您可以将其设置为HttpContext.CurrentPrincipal以便在请求范围内的任何位置使其可用。 The DelegatingHandler is called before your controllers as you can see in the image below, which makes it ideal for authentication logic. 在您的控制器之前调用DelegatingHandler ,如下图所示,这使其成为身份验证逻辑的理想选择。

在此输入图像描述

I would do the same on the client (write a DelegatingHandler or ActionFilterAttribute ) to add the authentication header on a default location. 我会在客户端上执行相同的操作(编写DelegatingHandlerActionFilterAttribute )以在默认位置添加身份验证标头。 Note that DelegatingHandler s are part of the HTTP pipeline and ActionFilterAttribute s belong to the MVC pipeline. 请注意, DelegatingHandler是HTTP管道的一部分, ActionFilterAttribute属于MVC管道。

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. 这可以像使用HTTPS上的基本身份验证一样简单,也可以像实现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. 注意: ASP.NET Web Api是基于REST的,因此您根本不想保留会话信息。

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. 编辑:有关如何实现处理基本身份验证的委托处理程序的示例,请参阅: 使用消息处理程序在asp.net web api中进行基本http身份验证。

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. 基本上,您需要将通过网络加密的用户名和密码发送到您的服务器应用程序,然后您可以让您的API生成随机会话ID并将其保存在列表(服务器端)中,并将ID发送回客户端。 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. 现在,每次客户端向服务器发送内容时,请在数据包中包含他收到的ID,这样服务器每次都可以检查它。

On client disconnection or fixed timeout you can remove the ID from the server list and ask the client to re-authenticate. 在客户端断开连接或固定超时时,您可以从服务器列表中删除ID并要求客户端重新进行身份验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM