作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理的应用程序与在远程设备上运行的现有应用程序进行交互。与远程应用程序的通信是通过其公共(public)网络服务进行的。我被要求构建一个增强功能,其中涉及客户端使用 Web 服务来处理需要安全传输的敏感数据。
谁能给我一些关于如何最好地进行的指示?
最佳答案
首先,您应该使用 SSL 并拒绝任何未使用它的请求。这将在数据通过 Internet 传输时对其进行加密。
如果您正在使用 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 服务传输数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4213663/
我是一名优秀的程序员,十分优秀!