Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set X509KeyStorageFlags for IIS environment. #21869

Open
wants to merge 1 commit into
base: prerel-9.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Runtime.InteropServices;

namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// https://github.com/dotnet/aspnetcore/blob/release/9.0/src/Servers/IIS/IIS/src/NativeMethods.cs
/// </summary>
static internal partial class NativeMethods
{
private const string KERNEL32 = "kernel32.dll";

private const string AspNetCoreModuleDll = "aspnetcorev2_inprocess.dll";

[LibraryImport(KERNEL32, EntryPoint = "GetModuleHandleW")]
private static partial IntPtr GetModuleHandle([MarshalAs(UnmanagedType.LPWStr)] string lpModuleName);

public static bool IsAspNetCoreModuleLoaded()
{
return GetModuleHandle(AspNetCoreModuleDll) != IntPtr.Zero;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;

Expand All @@ -12,6 +13,11 @@ public static OpenIddictServerBuilder AddProductionEncryptionAndSigningCertifica
throw new FileNotFoundException($"Signing Certificate couldn't found: {fileName}");
}

if (flag == null && OperatingSystem.IsWindows() && NativeMethods.IsAspNetCoreModuleLoaded())
{
flag = X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.EphemeralKeySet;
}

var certificate = flag != null
? X509CertificateLoader.LoadPkcs12FromFile(fileName, passPhrase, flag.Value)
: X509CertificateLoader.LoadPkcs12FromFile(fileName, passPhrase);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<TargetFramework>net9.0</TargetFramework>
<OutputType>Library</OutputType>
<IsPackable>true</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace />
</PropertyGroup>

Expand Down
Loading