Click here to Skip to main content
15,881,588 members
Articles / Web Development / ASP.NET

Central Key Management

Rate me:
Please Sign up or sign in to vote.
4.63/5 (18 votes)
8 Mar 200610 min read 63.8K   1.2K   49  
A central key manager for multiple web server clients in a web farm.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Core.DatabaseBlock;

namespace KeyManager
{
    class SessionKeyInfo
    {
        private string _keyData;
        private string _keyId;
        private string _wrappingCert;
        private string _description;
        private DateTime _expiration;
        private DateTime _creation;

        public DateTime Creation
        {
            get { return _creation; }
            set { _creation = value; }
        }

        public string KeyId
        {
            get { return _keyId; }
            set { _keyId = value; }
        }

        public string WrappingCert
        {
            get { return _wrappingCert; }
            set { _wrappingCert = value; }
        }

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        public DateTime Expiration
        {
            get { return _expiration; }
            set { _expiration = value; }
        } 

        public string KeyData
        {
          get { return _keyData; }
          set { _keyData = value; }
        }
   }

    class DataLayer : DataModuleCore
    {
        public DataLayer(string connectionString)
            : base( connectionString )
        {
        }

        public int StoreSessionKey(string key, string wrappingCert, DateTime expiration, string description)
        {
            using (DataCommand command = NewCommand("dbo.[InsertSessionKey]"))
            {
                command.NewParameter.StringParameter("@keyData", key);
                command.NewParameter.StringParameter("@wrappingCert", wrappingCert);
                command.NewParameter.DateTimeParameter("@expiration", expiration);
                command.NewParameter.StringParameter("@description", description);

                return command.ExecuteScalarInt();
            }
        }

        public SessionKeyInfo GetSessionKey(string key)
        {
            int keyId;
            
            if ( Int32.TryParse( key, out keyId ) == false )
                throw new ArgumentException( "Invalid KeyID." );

            SessionKeyInfo info = new SessionKeyInfo();
            using (DataCommand command = NewCommand("dbo.[GetSessionKey]"))
            {
                command.NewParameter.Int32Parameter("@keyId", keyId);

                DataReader reader = command.ExecuteReader();
                if (reader.Read() == true)
                {
                    info.KeyData = reader.ReadString("keyData");
                    info.KeyId = reader.ReadString("keyId");
                    info.Expiration = reader.ReadDateTime("expiration");
                    info.Creation = reader.ReadDateTime("creation");
                    info.Description = reader.ReadString("description");
                    info.WrappingCert = reader.ReadString("wrappingCert");
                }
            }

            return info;
        }

    }
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions