小编Myk*_*kun的帖子

我应该处理 X509Certificate2 吗?

我正在使用 IdentityServer4,我想从文件加载签名证书。例如,

var certificate = new X509Certificate2(
        path, 
        password, 
        X509KeyStorageFlags.EphemeralKeySet);

services.AddIdentityServer()
        .AddSigningCredential(certificate)
...
certificate.Dispose();
Run Code Online (Sandbox Code Playgroud)

当我从 IdentityServer 请求令牌时,上面的代码将不起作用。但它会在我删除certificate.Dispose();.

我也尝试了另一种选择。我RsaSecurityKey从证书的私钥创建并使用它来添加签名凭据。在这种情况下,处理不会破坏任何东西。

var rsk = new RsaSecurityKey(certificate.GetRSAPrivateKey()))

services.AddIdentityServer()
        .AddSigningCredential(rsk)
...
certificate.Dispose()
Run Code Online (Sandbox Code Playgroud)

所以我的问题更笼统。我应该处理X509Certificate2从现有证书创建的对象吗?


来自微软文档

从 .NET Framework 4.6 开始,此类型实现 IDisposable 接口。使用完类型后,应直接或间接处理它。

c# idisposable x509certificate2 identityserver4 asp.net-core-2.2

13
推荐指数
2
解决办法
2097
查看次数

我可以在.NET Core中获得私有构造函数吗?

在.NET Framework中,这种方法存在:

public ConstructorInfo GetConstructor(
    BindingFlags bindingAttr,
    Binder binder,
    CallingConventions callConvention,
    Type[] types,
    ParameterModifier[] modifiers
)
Run Code Online (Sandbox Code Playgroud)

但是在.NET Core(1.1)中,我只找到了扩展名,这没有给我私有构造函数:

public static ConstructorInfo GetConstructor(this Type type, Type[] types)
Run Code Online (Sandbox Code Playgroud)

所以我想知道我是否可以通过某种方式访问​​并获得.NET Core中的私有构造函数。

c# reflection .net-core

3
推荐指数
1
解决办法
138
查看次数