Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » XML » Web Services » Downloads
 

Implementing WS-SecureConversation in Microsoft IssueVision

By , 27 Sep 2005
 
issuevisionwsesmartclient.zip
Database
IssueVision
App.ico
Controls
Outlook
Images
IssueVision-about-banner.png
mosaic-light.png
Signin.png
statusbar
StatusConnected.ico
StatusDisconnected.ico
StatusSynchronizing.ico
toolbar
new_task.png
synchronize.png
work_offline.png
types
all_staffers.png
all_types.png
computer.png
environmental.png
help_desk.png
network.png
none.png
printer.png
staff.png
technicians.png
technicians_group.png
telecom.png
IssueVision.csproj.user
IssueVision.snk
Libraries
AppUpdater.dll
ThreadHelper Documentation
About BackgroundWorker.doc
Async Windows Forms Programming with BackgroundWorker.doc
ThreadHelper.dll
Panes
Patterns
Web References
localhost
IssueVisionServices.disco
IssueVisionServices.wsdl
Reference.map
IssueVisionWebWseCS
IssueVisionWebWseCS.csproj.webinfo
IVDataSet.xsx
Libraries
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 use 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

About the Author

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 27 Sep 2005
Article Copyright 2005 by Weidong Shen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid