我正在开发的应用程序与远程设备上运行的现有应用程序接口(interface)。与远程应用程序的通信是通过其公共(public)网络服务进行的。我被要求构建一个增强功能,其中涉及客户端利用 Web 服务来处理需要安全传输的敏感数据。
有人可以给我一些关于如何最好地进行的指示吗?
最佳答案
首先,您应该使用 SSL 并拒绝任何不使用它的请求。这将在数据通过互联网传输时对其进行加密。
如果您使用 SOAP,您可以在服务中定义一个采用用户名/密码的自定义 header 。然后,对于每个公共(public)方法中的第一行,根据数据库验证用户名和密码。如果成功,请适当设置 HttpContext.Current.User,您的服务将与内置的 Asp.NET 基础结构很好地结合在一起。
添加:下面是一个示例 SoapHeader,其中包含用于身份验证的用户名/密码。
// define the header
public class AuthenticationHeader : SoapHeader
{
public String UserName { get; set; }
public String Password { get; set; }
}
// your service
public class PublicWebService : WebService
{
// defines an instance of the header as part of the service
public AuthenticationHeader Authentication;
private void Authenticate()
{
// validate the username / password against a database
// set the HttpContext.Current.User if successful.
// Maybe throw a SoapException() if authentication fails
}
// Notice the SoapHeader("Authentication") attribute...
// This tells ASP.Net to look for the incoming header for this method...
[WebMethod]
[SoapHeader("Authentication")]
public void PublicMethod1()
{
Authenticate();
// your code goes here
}
// Expose another method with the same authentication mechanism
[WebMethod]
[SoapHeader("Authentication")]
public void PublicMethod2()
{
Authenticate();
// your code goes here
}
}
现在,如果运行 wsdl 工具,生成的代理类将包含定义的身份验证 header :
PublicWebService s = new PublicWebService();
s.Authentication = new AuthenticationHeader();
s.Authentication.UserName = "xxxxxxxx";
s.Authentication.Password = "yyyyyyyy";
s.PublicMethod1();
s.PublicMethod2();
关于c# - 如何安全地将数据传输到公共(public) Web 服务/从公共(public) Web 服务传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4213663/