Encryption Utility based on MachineKey

This is a supersimple utility class to encrypt data in .net

Obviously you need to have the same MachineKey in your config to be able to decrypt it…

using System;
using System.Configuration;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Web.Configuration;

namespace SdbBackbone.Encryption
{
    public static class EncrytionService
    {
        // This constant string is used as a "salt" value for the PasswordDeriveBytes function calls.
        // This size of the IV (in bytes) must = (keysize / 8).  Default keysize is 256, so the IV must be
        // 32 bytes long.  Using a 16 character string here gives us 32 bytes when converted to a byte array.
        private const string initVector = "tu89geji340t89u2";

        // This constant is used to determine the keysize of the encryption algorithm.
        private const int keysize = 256;

        public static string ValidationKey
        {
            get
            {
                //Retrieves a specified configuration section for the current application's default configuration.
                MachineKeySection section = (MachineKeySection) ConfigurationManager.GetSection("system.web/machineKey");
                return section.ValidationKey;
                // return ConfigurationManager.GetSection("system.web/machineKey/validationKey").ToString();
            }
        }

        public static string Encrypt(string plainText )
        {
            return Encrypt(plainText, ValidationKey);
        }

        private static string Encrypt(string plainText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            var password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize/8);
            var symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
            var memoryStream = new MemoryStream();
            var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] cipherTextBytes = memoryStream.ToArray();
            memoryStream.Close();
            cryptoStream.Close();
            return Convert.ToBase64String(cipherTextBytes);
        }

        public static string Decrypt(string plainText)
        {
            return Decrypt(plainText, ValidationKey);
        }
        private static string Decrypt(string cipherText, string passPhrase)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            var password = new PasswordDeriveBytes(passPhrase, null);
            byte[] keyBytes = password.GetBytes(keysize/8);
            var symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
            var memoryStream = new MemoryStream(cipherTextBytes);
            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            var plainTextBytes = new byte[cipherTextBytes.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
        }
    }
}

Migrate Users Across Sitecore Databases

This script is used to migrate users in a specific role from two different Sitecore Instances.

As you can imagine, it runs on the Core databases.

begin tran
declare @roleId  uniqueidentifier
declare @destinationRoleId  uniqueidentifierselect TOP 1 @roleId  =RoleId from dbo.aspnet_Roles where LoweredRoleName=’sitecore\sourcegroup’select @roleId

select TOP 1 @destinationRoleId =RoleId from [OtherDB].dbo.aspnet_Roles where LoweredRoleName=’sitecore\destinationgroup’
select @destinationRoleId
select *from aspnet_UsersInRoles where RoleId=  @roleId

select *from aspnet_Roles where RoleId=  @roleId

select *from [OtherDB].dbo.aspnet_Roles where RoleId=  @destinationRoleId

select distinct sourceUsers.ApplicationId, sourceUsers.UserId,sourceUsers.UserName,sourceUsers.LoweredUserName,sourceUsers.MobileAlias,sourceUsers.IsAnonymous,sourceUsers.LastActivityDate
into #TmpUsers
from dbo.aspnet_Users as sourceUsers
inner join dbo.aspnet_UsersInRoles on sourceUsers.UserId = aspnet_UsersInRoles.UserId
where aspnet_UsersInRoles.RoleId=@roleId
and sourceUsers.LoweredUserName not in (select distinct LoweredUserName from  [OtherDB].dbo.aspnet_Users )

select * from #TmpUsers

INSERT into [OtherDB].dbo.aspnet_Users
select * from #TmpUsers

INSERT into [OtherDB].dbo.aspnet_UsersInRoles
select UserId, @destinationRoleId as RoleId from #TmpUsers

select distinct gcu.ApplicationId, gcu.UserId,gcu.UserName,gcu.LoweredUserName,gcu.MobileAlias,gcu.IsAnonymous,gcu.LastActivityDate
into #TmpExistingUsers
from dbo.aspnet_Users as sourceUsers inner join dbo.aspnet_UsersInRoles on sourceUsers.UserId = aspnet_UsersInRoles.UserId
inner join [OtherDB].dbo.aspnet_Users gcu on gcu.LoweredUserName = sourceUsers.LoweredUserName
where aspnet_UsersInRoles.RoleId=@roleId

select * from #TmpExistingUsers

INSERT into [OtherDB].dbo.aspnet_UsersInRoles
select ExistingUsers.UserId, @destinationRoleId as RoleId from #TmpExistingUsers ExistingUsers
left outer join [OtherDB].dbo.aspnet_UsersInRoles   on ExistingUsers.UserId = [OtherDB].dbo.aspnet_UsersInRoles.UserId and [OtherDB].dbo.aspnet_UsersInRoles.RoleId = @destinationRoleId
where [OtherDB].dbo.aspnet_UsersInRoles.UserId is null

drop table #TmpUsers

drop table #TmpExistingUsers

select *from [OtherDB].dbo.aspnet_UsersInRoles where RoleId=  @destinationRoleId

–rollback tran
commit tran