views:

832

answers:

2

Does anyone know of examples which show how to encrypt a dataset at the client side and send it over to a web service and have it decrypted there?

Another question: What i need to do is a send hundreds of rows of data from a client to a web service and have the web service update the database with these records. I can't think of any other way to do this without using a dataset. Is there a better method?

Thanks in advance!

A: 

Well, there are a lot of approaches to this. Since you are sending this over the wire, you could: 1) Write the data to an XML stream (this is very much what the DataSet is meant to do) then 2) you could compress the XML (the compression ratio would be best at this stage) then 3) Encrypt using one of the .NET cryptographic schemes and finally 4) decrypt, unzip, and deserialize your XML into a DataSet object or whatever you want to do with it.

Note, you might need to Base64 the result of the encryption. Another alternative is to not encrypt and use the Web Service over SSL and just use that native encryption. Depending on the type of data a DataSet may not be the best choice in terms of performance. You could send a CSV or JSON style data block; this would potentially be smaller, especially if there is only one "DataTable" object in your DataSet. A lot of this is situation dependent as far as what the best method do use is.

BobbyShaftoe
+2  A: 

As far as the encryption is concerned, why try to reinvent the wheel? Just connect to the webservice over SSL - it'll most likely be much safer than a homegrown alternative.

I would probably create a custom struct/object and send an array of those to the webservice rather than a DataSet. It will mean (slightly) less network traffic; it will make the webservice's WSDL more descriptive; and it will make it easier for any non-Microsoft apps to talk to the webservice, if that becomes necessary in the future.

EDIT: An example...

At the server-side you can declare a custom type (eg, ExampleUser), and then setup your method to accept an array of that type instead of a DataSet:

[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...
    }
}

In the client application you would add a reference to the webservice. This will enable you to use the ExampleUser type declared in the server-side code above.

You could then just convert your DataSet to an array of ExampleUser objects before sending it to the webservice:

// 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);

EDIT: Another example...

If you're using .NET 3.5 then you can get the client-side down to just a few lines of code by using LINQ and object initialisers to create your array:

// 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);
LukeH
thanks for the example.This helps alot!
zSysop