Encryption .Net Web服务加密解密数据集

Encryption .Net Web服务加密解密数据集,encryption,service,dataset,Encryption,Service,Dataset,有人知道如何在客户端加密数据集并将其发送到web服务并在那里解密的示例吗 另一个问题: 我需要做的是从客户机向web服务发送数百行数据,并让web服务使用这些记录更新数据库。如果不使用数据集,我想不出任何其他方法来实现这一点。有更好的方法吗 提前谢谢 嗯,有很多方法可以做到这一点。因为您是通过网络发送的,所以您可以:1)将数据写入XML流(这正是数据集要做的),然后2)压缩XML(压缩比在这个阶段最好),然后3)使用.NET加密方案之一进行加密,最后4)解密、解压缩,并将XML反序列化为Data

有人知道如何在客户端加密数据集并将其发送到web服务并在那里解密的示例吗

另一个问题: 我需要做的是从客户机向web服务发送数百行数据,并让web服务使用这些记录更新数据库。如果不使用数据集,我想不出任何其他方法来实现这一点。有更好的方法吗


提前谢谢

嗯,有很多方法可以做到这一点。因为您是通过网络发送的,所以您可以:1)将数据写入XML流(这正是数据集要做的),然后2)压缩XML(压缩比在这个阶段最好),然后3)使用.NET加密方案之一进行加密,最后4)解密、解压缩,并将XML反序列化为DataSet对象或您想对其执行的任何操作


注意,您可能需要将加密的结果设置为Base64。另一种选择是不通过SSL加密和使用Web服务,而只使用本机加密。根据数据类型的不同,数据集可能不是性能方面的最佳选择。您可以发送CSV或JSON样式的数据块;这可能会更小,尤其是当数据集中只有一个“DataTable”对象时。就使用的最佳方法而言,这在很大程度上取决于具体情况。

就加密而言,为什么要尝试重新发明轮子?只需通过SSL连接到webservice,它很可能比自主开发的替代方案安全得多

我可能会创建一个自定义结构/对象,并将这些结构/对象的数组发送到Web服务,而不是数据集。这将意味着(稍微)更少的网络流量;它将使Web服务的WSDL更具描述性;如果将来有必要的话,这将使任何非微软应用程序更容易与Web服务对话

编辑:示例…

在服务器端,您可以声明自定义类型(例如ExampleUser),然后将方法设置为接受该类型的数组而不是数据集:

[WebService(Namespace="http://example.yourdomain.com/ExampleWebService/")]
public class ExampleWebService : System.Web.Services.WebService
{
    // this is your custom type
    public class ExampleUser
    {
        public int UserID { get; set; }
        public string Name { get; set; }
        public DateTime DateOfBirth { get; set; }
    }

    // this is your method
    // accepts an array of ExampleUser rather than a DataSet
    [WebMethod]
    public void UploadUsers(ExampleUser[] usersArray)
    {
        // do something...
    }
}
在客户端应用程序中,您将添加对Web服务的引用。这将使您能够使用上面服务器端代码中声明的ExampleUser类型

然后,您可以将数据集转换为ExampleUser对象数组,然后再将其发送到Web服务:

// get the number of rows in the DataTable
int rowCount = yourDataSet.Tables[0].Rows.Count;

// create an array of ExampleUser with the correct capacity
ExampleWebService.ExampleUser[] usersArray =
    new ExampleWebService.ExampleUser[rowCount];

// iterate through each row in the table
for (int i = 0; i < rowCount; i++)
{
    DataRow dr = yourDataSet.Tables[0].Rows[i];

    // create an ExampleUser object and populate it from the DataRow columns
    ExampleWebService.ExampleUser eu = new ExampleWebService.ExampleUser();
    eu.UserID = (int)dr["User_ID"];
    eu.Name = (string)dr["Name"];
    eu.DateOfBirth = (DateTime)dr["Date_Of_Birth"];

    // add the ExampleUser object to the array
    usersArray[i] = eu;
}

// the array is populated so let's call the webservice
ExampleWebService.UploadUsers(usersArray);
// create and populate the array
ExampleWebService.ExampleUser[] usersArray =
    yourDataSet.Tables[0].AsEnumerable().Select
    (
        s => new ExampleWebService.ExampleUser()
        {
            UserID = (int)s["User_ID"],
            Name = (string)s["Name"],
            DateOfBirth = (DateTime)s["Date_Of_Birth"]
        }
    ).ToArray();

// the array is populated so let's call the webservice
ExampleWebService.UploadUsers(usersArray);