From abfa4de2e09f596ae324c86a70f6ba865e15bea8 Mon Sep 17 00:00:00 2001
From: unknown <Eskandari@.(none)>
Date: Mon, 26 Apr 2010 11:34:05 +0430
Subject: [PATCH] Fixed running license generator in web context

---
 Rhino.Licensing/LicenseGenerator.cs    |   25 +++++++++++++++++++++++--
 Rhino.Licensing/Rhino.Licensing.csproj |    1 +
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/Rhino.Licensing/LicenseGenerator.cs b/Rhino.Licensing/LicenseGenerator.cs
index a6c89c2..6eaa63c 100644
--- a/Rhino.Licensing/LicenseGenerator.cs
+++ b/Rhino.Licensing/LicenseGenerator.cs
@@ -20,7 +20,7 @@ namespace Rhino.Licensing
 
 		public string GenerateFloatingLicense(string name, string publicKey)
 		{
-			using (var rsa = new RSACryptoServiceProvider())
+			using (var rsa = CreateNewCryptoService())
 			{
 				rsa.FromXmlString(privateKey);
 				var doc = new XmlDocument();
@@ -56,7 +56,7 @@ namespace Rhino.Licensing
 
 		public string Generate(string name, Guid id, DateTime expirationDate, IDictionary<string, string> attributes, LicenseType licenseType)
         {
-            using (var rsa = new RSACryptoServiceProvider())
+            using (var rsa = CreateNewCryptoService())
             {
                 rsa.FromXmlString(privateKey);
                 var doc = CreateDocument(id, name, expirationDate, attributes, licenseType);
@@ -76,6 +76,27 @@ namespace Rhino.Licensing
             }
         }
 
+        private RSACryptoServiceProvider CreateNewCryptoService()
+        {
+            var param = new CspParameters
+            {
+                Flags = CspProviderFlags.UseMachineKeyStore
+            };
+
+            RSACryptoServiceProvider rsa;
+
+            if (System.Web.HttpContext.Current == null) // Not a web context
+            {
+                rsa = new RSACryptoServiceProvider();
+            }
+            else //web context
+            {
+                rsa = new RSACryptoServiceProvider(param);
+            }
+
+            return rsa;
+        }
+
         private static XmlElement GetXmlDigitalSignature(XmlDocument x, AsymmetricAlgorithm key)
         {
             var signedXml = new SignedXml(x) { SigningKey = key };
diff --git a/Rhino.Licensing/Rhino.Licensing.csproj b/Rhino.Licensing/Rhino.Licensing.csproj
index 6786102..b11ed78 100644
--- a/Rhino.Licensing/Rhino.Licensing.csproj
+++ b/Rhino.Licensing/Rhino.Licensing.csproj
@@ -46,6 +46,7 @@
     <Reference Include="System.ServiceModel">
       <RequiredTargetFramework>3.0</RequiredTargetFramework>
     </Reference>
+    <Reference Include="System.Web" />
     <Reference Include="System.Xml.Linq">
       <RequiredTargetFramework>3.5</RequiredTargetFramework>
     </Reference>
-- 
1.6.5.1.1367.gcd48

