简体   繁体   English

如何保护.NET中的Web服务免受单个应用程序以外的外部环境的侵害?

[英]How to protect a webservice in .NET from outer world except a single application?

I have a website with architecture (HTML + JSON + webservice (C#)) installed on a server which is open to internet. 我有一个网站,该网站的体系结构(HTML + JSON + Webservice(C#))安装在对互联网开放的服务器上。 Now, my webservices are opened to the whole world so anyone can access it and may try to malfunction. 现在,我的Web服务已向全世界开放,因此任何人都可以访问它,并可能会出现故障。

What I would like to do is to make my webservice to work limitedly to my application only instead of other applications. 我想做的是使我的Web服务只能在我的应用程序中工作,而不能在其他应用程序中工作。 Like when a website is on open internet but its web services are private to the website only and not to the whole internet. 就像网站在开放Internet上,但其Web服务仅对网站私有,而不对整个Internet私有一样。

Right now there's a big data security concern. 目前,存在一个很大的数据安全问题。

I would suggest you look into using Certificates to encrypt your webservice, then your application will call it using that same loaded certificate. 我建议您考虑使用“证书”对Web服务进行加密,然后您的应用程序将使用相同的已加载证书来调用它。 Only an entity with your server certificate key will be able to decrypt your calls. 只有拥有服务器证书密钥的实体才能解密您的呼叫。

Accessing a web service and a HTTP interface using certificate authentication 使用证书认证访问Web服务和HTTP接口

Calling a rest api with username and password - how to 使用用户名和密码调用Rest API-如何

Edit: If you cannot use the certificate method because you are calling from say the browser directly, you might want to look at an authorization cookie of some sort, So that lets say on your login pages the request might be open to public but on all subsequent request you require authentication and once the user has logged in you rely on the authorization cookie or token to validate whether they have access. 编辑:如果由于直接从浏览器调用而无法使用证书方法,则可能需要查看某种授权cookie,因此可以在登录页面上说该请求可能对公众开放,但所有在随后的请求中,您需要进行身份验证,并且一旦用户登录,您就可以依靠授权cookie或令牌来验证他们是否具有访问权限。

Another method: IdentityServer for instance has provider token stores, so you can request a token from the store, then only with the issued token you can access the API. 另一种方法:例如IdentityServer具有提供者令牌存储,因此您可以从存储中请求令牌,然后只有使用已发行的令牌,您才能访问API。 And your API would also then query the store to check the token is valid. 然后,您的API还将查询商店以检查令牌是否有效。

I found a very easy solution to it.. by getting ip address of the remote client's in web service i can validate whether the request is coming from my webserver or not 我找到了一个非常简单的解决方案..通过获取Web服务中远程客户端的IP地址,我可以验证请求是否来自我的Web服务器。

so anytime if any spamming client try to request my web service i will retrieve its ip on every request inside webservice and reject the request for ex : 因此,只要有垃圾邮件发送者尝试请求我的Web服务,我都会在webservice内的每个请求上检索其ip并拒绝ex的请求:

if (HttpContext.Current.Request.UserHostAddress.ToString() == "127.0.0.1")
{
   return "my server";
}
else
{
  return "invalid client or spammer"
}

The primary way would be to add some sort off login/account system. 主要方法是添加某种分类的登录/帐户系统。 That then of course needs to be managed and all that. 那么那当然需要加以管理。

The other posters' approaches of just putting some key or certificate into it will only work if it is not that important. 其他张贴者仅将一些密钥或证书放入其中的方法只有在其不那么重要时才起作用。 Anything you store in the application is invariably reverse-engineerable. 您存储在应用程序中的任何内容都可以进行反向工程。 So if someone is decently determined, that level of protection is easy enough to overcome. 因此,如果某人有一定的决心,那么可以轻松克服这一保护级别。

If the number of possible consumers is small enough, another approach might work: Make it only reachable from the local network (IP adress ranges associated with Local Networks). 如果可能的使用者数量足够少,则可以使用另一种方法:使它只能从本地网络访问(与本地网络关联的IP地址范围)。 Then use a approach like VPN so clients can connect to you local Network. 然后使用类似VPN的方法,以便客户端可以连接到您的本地网络。 Basically you move the account part to another System. 基本上,您将帐户部分移至另一个系统。 But such a approach can have security issues, especially if that VPN connection goes straight into your DMZ. 但是这种方法可能会带来安全问题,尤其是如果该VPN连接直接进入您的DMZ。

In the end it really depends how critical that is. 最后,这实际上取决于关键程度。 Security exists in flavors from "anybody reading his computers network traffic with Wireshark can break it" to "reasonably secure for high grade private data". 从“使用Wireshark读取计算机网络流量的任何人都可以破坏它”到“为高级私人数据提供合理的安全性”方面,都存在安全性。 We have no idea what level of security you need and what level of skill you have, so we can not give a useful answer just yet. 我们不知道您需要什么级别的安全性以及您拥有什么级别的技能,因此我们尚无法给出有用的答案。

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

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