2011-12-01 91 views
1

这里是我的小web服务演示:如何加密对WebService ASMX的响应/请求?

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
public class ServicePrueba : System.Web.Services.WebService 
{ 
    [WebMethod(EnableSession=true)] 
    public int SayHello() 
    { 
     int counter; 
     if (Context.Session == null) 
     { 
      counter = 1; 
     } 
     else 
     { 
      int tmpCounter = Convert.ToInt32(Context.Session["Counter"]); 
      counter = tmpCounter + 1; 
     } 

     Context.Session["Counter"] = counter; 
     return counter; 
    } 
} 

这里就是我如何通过一个控制台客户端应用程序调用它:

class Program 
{ 
    static void Main(string[] args) 
    { 
     ServicePrueba serviceProxy = new ServicePrueba(); 

     /*Set the cookie container on the proxy.*/ 
     var cookies = new System.Net.CookieContainer(); 
     serviceProxy.CookieContainer = cookies; 

     Console.WriteLine(serviceProxy.SayHello()); 
     Console.ReadKey(); 

     Console.WriteLine(serviceProxy.SayHello()); 
     Console.ReadKey(); 

     Console.WriteLine(serviceProxy.SayHello()); 
     Console.ReadKey(); 

     Console.WriteLine(serviceProxy.SayHello()); 
     Console.ReadKey(); 

     Console.WriteLine(serviceProxy.SayHello()); 
     Console.ReadKey(); 

     Console.ReadKey(true); 
    } 
} 

我的问题是,信息是人来人往通过导线是纯文本没有? (有没有办法看到这些数据是什么样子的?)

如何保护这些敏感信息免受窥视?我想简单地加密消息,因此当它通过线路时它是乱码。

+0

有一个类似的问题,它提供了相当有用的答案http://stackoverflow.com/questions/18448/encryption-in-c-sharp-web-services 但我认为最简单的方法是只使用https – dasheddot

回答

3

您应该使用HTTPS。通过HTTPS连接托管它可以解决此问题。

+0

所以如果我使用SSL来使用HTTPS,没有人可以获取这些信息? –

+0

正确。通过SSL,所有可能敏感的内容都被加密。唯一可能被认为是敏感的是目标服务器。所以他们会知道你连接到谁,但理论上不能得到任何额外的信息。 – McKay