c# - 如何安全地将数据传输到公共(public) Web 服务/从公共(public) Web 服务传输数据

标签 c# .net web-services webservices-client

我正在开发的应用程序与远程设备上运行的现有应用程序接口(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/

相关文章:

c# - 有效处理 C# 方法中的单个值和多个值

java - 如何在 REST 端点上排队 REST 请求/暂停处理一段时间而不丢失请求?

c# - 针对 Windows 文件的服务器端病毒扫描

c# - ConfigurationCollection - 无法识别的元素 'entry'

c# - 有没有办法无限期地暂停线程?

java - CXF 用户名 token 问题

c# - 动态创建 CAML 查询

c# - 考虑到有效数字的数量,如何找到浮点值的最小有效步长?

c# - 模拟以更新 ASP.NET 表单例份验证站点中的用户 AD 信息

c# - 使用 Microsoft.DirectX.AudioVideoPlayback 播放视频时出错;汇编版本错误