英文:
How to authenticate a request coming to WEB API
问题
我有一个WEB API,接收来自多个应用程序的请求。基于接收到的JSON,WEB API的逻辑决定要处理的步骤,最终发送通知给相关负责人。
我需要保护我的WEB API,以防未经授权的系统发送请求到WEB API,可能导致不必要的处理和发送不需要的消息给接收者。
基本上,我正在尝试避免WEB API受到攻击。我有一个想法,为了做到这一点,客户端系统必须在请求头中发送一个客户端ID,WEB API将需要进行身份验证。如何实现这个要求的最佳方法是什么?如何做到这一点?
如果能够获得如何实现这一点以及如何创建客户端ID并对请求WEB API的客户端进行身份验证的详细想法,我将不胜感激。
英文:
I have a WEB API that receives requests from several applications. Based on the JSON
received, the WEB API
logic decides the steps to process and finally sends out notifications to individuals responsible.
I need to secure my WEB API so that unauthorized systems do not send requests to the WEB API, which might lead to unwanted processing and sending unwanted messages to recipients.
Basically I am trying to avoid the WEB API being attacked. I have an idea in order to do that the client systems must send a client-id in the request header in which the WEB API will have to authenticate. What is the best approach to achieve this requirement? and How to do this?
I would be grateful if I can get a detailed idea of how to achieve this and how to create client id's, and authenticate the clients requesting the WEB API.
答案1
得分: 1
如果您的Web API仅接收某些应用程序,您可以使用AuthorizationFilterAttribute应用IP过滤器。
public class IpFilter : AuthorizationFilterAttribute
{
string[] allowedIps;
public IpFilter()
{
allowedIps = ReadFromConfig(); //您可以从配置中读取允许的IP地址
}
public override void OnAuthorization(HttpActionContext actionContext)
{
CheckIps(actionContext);
}
public void CheckIps(HttpActionContext actionContext)
{
string requestIpAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(requestIpAddress))
{
requestIpAddress = HttpContext.Current.Request.UserHostAddress;
}
if (string.IsNullOrEmpty(requestIpAddress))
throw new HttpResponseException(HttpStatusCode.Unauthorized);
foreach (var ip in allowedIps)
{
if (requestIpAddress == ip.Trim())
return;
}
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,
new { Message = "未经授权" });
}
}
然后,您只需要将此属性添加到控制器。
[IpFilter]
public class YouController : ApiController
{
}
英文:
If your web api only receives some applications you can apply IP filter with AuthorizationFilterAttribute.
public class IpFilter : AuthorizationFilterAttribute
{
string[] allowedIps;
public IpFilter()
{
allowedIps = ReadFromConfig(); //You may read from config your allowed ips
}
public override void OnAuthorization(HttpActionContext actionContext)
{
CheckIps(actionContext);
}
public void CheckIps(HttpActionContext actionContext)
{
string requestIpAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(requestIpAddress ))
{
requestIpAddress = HttpContext.Current.Request.UserHostAddress;
}
if(string.IsNullOrEmpty(requestIpAddress))
throw new HttpResponseException(HttpStatusCode.Unauthorized);
foreach (var ip in allowedIps)
{
if (requestIpAddress == ip.Trim())
return;
}
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized,
new { Message = "Unauthorized" });
}
}
Then all you need to do is adding this attribute to controller.
[IpFilter]
public class YouController : ApiController
{
}
答案2
得分: 1
首先,让我们搞清楚这些概念。
身份验证 => 关于身份识别
授权 => 关于权限
身份验证意味着确认您自己的身份,而授权意味着被允许访问系统。
-
您可以使用 JWT 来在标头中发送客户端标识、数字等(已编码)。
-
在JWT中,您的令牌标头中有一个秘密密钥(已编码),防止其他用户访问您的API。
英文:
First of all, let's make these concepts clear.
> Authentication => About Identification
>
> Authorization => About Permission
Authentication means confirming your own identity, whereas authorization means being allowed access to the system.
-
You can use JWT for both of them to send client-id, number etc.(encoded) in header.
-
In JWT, you have a secret key(encoded) in your token header, that prevents other users to access your API.
答案3
得分: 0
任何对这个问题投下负评的人都必须附上负评的理由!我提出这个问题是因为在我看来这是一个合理的问题。如果不是的话,在Stackoverflow上提问问题有什么意义呢?管理员能否调查此事?
英文:
Anybody who down votes this question have to add the reason for down voting ! I am asking a question since its a valid question from my point of view. If not whats the point of asking questions in Stackoverflow. Can administrators look into this ?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论