From 7b4e1fd48e91408eb039b7f565ca8bfbb8fb030a Mon Sep 17 00:00:00 2001 From: David Fowler Date: Tue, 29 Apr 2014 23:58:53 -0700 Subject: [PATCH] Added DPAPI implementation that works on mono --- .../HostingServices.cs | 19 ++++++++++++++----- .../Microsoft.AspNet.Hosting.kproj | 1 + .../PlatformHelper.cs | 17 +++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/Microsoft.AspNet.Hosting/PlatformHelper.cs diff --git a/src/Microsoft.AspNet.Hosting/HostingServices.cs b/src/Microsoft.AspNet.Hosting/HostingServices.cs index 4ef1e162c6..64f50b58ad 100644 --- a/src/Microsoft.AspNet.Hosting/HostingServices.cs +++ b/src/Microsoft.AspNet.Hosting/HostingServices.cs @@ -37,11 +37,20 @@ namespace Microsoft.AspNet.Hosting Lifecycle = LifecycleKind.Scoped }; - // The default IDataProtectionProvider is a singleton. - // Note: DPAPI isn't usable in IIS where the user profile hasn't been loaded, but loading DPAPI - // is deferred until the first call to Protect / Unprotect. It's up to an IIS-based host to - // replace this service as part of application initialization. - yield return describer.Instance(DataProtectionProvider.CreateFromDpapi()); + if (PlatformHelper.IsMono) + { +#if NET45 + yield return describer.Instance(DataProtectionProvider.CreateFromLegacyDpapi()); +#endif + } + else + { + // The default IDataProtectionProvider is a singleton. + // Note: DPAPI isn't usable in IIS where the user profile hasn't been loaded, but loading DPAPI + // is deferred until the first call to Protect / Unprotect. It's up to an IIS-based host to + // replace this service as part of application initialization. + yield return describer.Instance(DataProtectionProvider.CreateFromDpapi()); + } } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/Microsoft.AspNet.Hosting.kproj b/src/Microsoft.AspNet.Hosting/Microsoft.AspNet.Hosting.kproj index de22702e86..5e436ba601 100644 --- a/src/Microsoft.AspNet.Hosting/Microsoft.AspNet.Hosting.kproj +++ b/src/Microsoft.AspNet.Hosting/Microsoft.AspNet.Hosting.kproj @@ -29,6 +29,7 @@ + diff --git a/src/Microsoft.AspNet.Hosting/PlatformHelper.cs b/src/Microsoft.AspNet.Hosting/PlatformHelper.cs new file mode 100644 index 0000000000..2d2f0adeed --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/PlatformHelper.cs @@ -0,0 +1,17 @@ +using System; + +namespace Microsoft.AspNet.Hosting +{ + internal static class PlatformHelper + { + private static Lazy _isMono = new Lazy(() => Type.GetType("Mono.Runtime") != null); + + public static bool IsMono + { + get + { + return _isMono.Value; + } + } + } +} \ No newline at end of file