Click here to Skip to main content
15,892,005 members
Articles / Web Development / HTML

Implementing WS-SecureConversation in Microsoft IssueVision

Rate me:
Please Sign up or sign in to vote.
4.61/5 (12 votes)
27 Sep 20056 min read 73.2K   776   38  
Adding secure communications to the Microsoft IssueVision sample application using WSE 2.0.
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace IssueVision
{
	// The SerializationHelper class contains Serialize / Deserialize methods for 
	// storing data on the client machine securely.  Data objects are first serialized
	// to a binary stream, then encrypted, then written to disk (typically in the
	// UserApplicationDataPath location).
	public class SerializationHelper 
	{
		public static void Serialize(object data, string filePath) 
		{
			try 
			{
				// 1. Open the file
				StreamWriter fs = new StreamWriter(filePath, false);
	            
				try 
				{
					MemoryStream streamMemory = new MemoryStream();
					BinaryFormatter formatter = new BinaryFormatter();
					
					// 2. Serialize the dataset object using the binary formatter
					formatter.Serialize(streamMemory, data);
					
					// 3. Encrypt the binary data
					string binaryData = Convert.ToBase64String(streamMemory.GetBuffer());
					string cipherData = DataProtection.Encrypt(binaryData, DataProtection.Store.User);
					
					// 4. Write the data to a file
					fs.Write(cipherData);
				} 
				finally 
				{
					// 5. Close the file
					fs.Flush();
					fs.Close();
				}
			} 
			catch
			{
				// Eat exception if this fails.
			}
		}

		public static object Deserialize( string  filePath) 
		{
			object data = new object();

			try 
			{
				// 1. Open the file
				StreamReader sr = new StreamReader(filePath);
		        
				try 
				{
					MemoryStream streamMemory;
					BinaryFormatter formatter = new BinaryFormatter();
					
					// 2. Read the binary data, and convert it to a string
					string cipherData = sr.ReadToEnd();
					
					// 3. Decrypt the binary data
					byte[] binaryData = Convert.FromBase64String(DataProtection.Decrypt(cipherData, DataProtection.Store.User));
					
					// 4. Rehydrate the dataset
					streamMemory = new MemoryStream(binaryData);
					data = formatter.Deserialize(streamMemory);
				} 
				catch 
				{ 
					// data could not be deserialized
					data = null;
				} 
				finally 
				{
					// 5. Close the reader
					sr.Close();
				}
			} 
			catch 
			{
				// file doesn't exist
				data = null;
			}

			return data;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET

Comments and Discussions