Browse Source

Set `X509KeyStorageFlags` for `IIS` environment.

Resolve #19150
pull/21869/head
maliming 1 year ago
parent
commit
3f6d84a4d5
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 22
      modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Microsoft/Extensions/DependencyInjection/NativeMethods.cs
  2. 6
      modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Microsoft/Extensions/DependencyInjection/OpenIddictServerBuilderExtensions.cs
  3. 1
      modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo.Abp.OpenIddict.AspNetCore.csproj

22
modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Microsoft/Extensions/DependencyInjection/NativeMethods.cs

@ -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;
}
}

6
modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Microsoft/Extensions/DependencyInjection/OpenIddictServerBuilderExtensions.cs

@ -1,3 +1,4 @@
using System;
using System.IO; using System.IO;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
@ -12,6 +13,11 @@ public static class OpenIddictServerBuilderExtensions
throw new FileNotFoundException($"Signing Certificate couldn't found: {fileName}"); 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 var certificate = flag != null
? X509CertificateLoader.LoadPkcs12FromFile(fileName, passPhrase, flag.Value) ? X509CertificateLoader.LoadPkcs12FromFile(fileName, passPhrase, flag.Value)
: X509CertificateLoader.LoadPkcs12FromFile(fileName, passPhrase); : X509CertificateLoader.LoadPkcs12FromFile(fileName, passPhrase);

1
modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo.Abp.OpenIddict.AspNetCore.csproj

@ -7,6 +7,7 @@
<TargetFramework>net9.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<IsPackable>true</IsPackable> <IsPackable>true</IsPackable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<RootNamespace /> <RootNamespace />
</PropertyGroup> </PropertyGroup>

Loading…
Cancel
Save