相关疑难解决方法(0)

使用csproj dotnetcore将文件复制到输出目录

所以我的问题非常简单.我有一些文件要复制到构建输出目录,无论是调试版本还是发布版本.我能找到的所有信息都是关于旧的json配置方法.任何人都有一个使用csproj与dotnetcore的例子?

c# .net-core

67
推荐指数
7
解决办法
4万
查看次数

59
推荐指数
1
解决办法
5万
查看次数

向 .NET Core 3 应用程序添加身份服务器身份验证失败,并显示“未指定密钥类型”。

我正在尝试向 .NET Core 3 API 项目添加身份服务器身份验证。

我已经添加了这个代码

public void ConfigureServices(IServiceCollection services)
{
    … 

    var identityBuilder = services.AddIdentityServer();

    identityBuilder.AddApiAuthorization<ApplicationUser, DbContext>();

    services
        .AddAuthentication()
        .AddIdentityServerJwt();

    var fileName = Path.Combine("Certificates", "certificatefile.pfx");
    var cert = new X509Certificate2(fileName, "veryDifficultPassword");
    identityBuilder.AddSigningCredential(cert);

    … 
}
Run Code Online (Sandbox Code Playgroud)

和:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    … 
    app.UseAuthentication();
    app.UseIdentityServer(); // <--- this line throws error.
    app.UseAuthorization();
    … 
}

Run Code Online (Sandbox Code Playgroud)

文件/Certificates夹中有一个文件可以正确读取和加载- 我可以检查cert变量并且它看起来是正确的。

到目前为止,我所尝试的一切都以线路app.UseIdentityServer();炸毁而告终:

System.InvalidOperationException: '未指定密钥类型。'

有什么建议?

更新:包括堆栈跟踪

System.InvalidOperationException
  HResult=0x80131509
  Message=Key type not specified.
  Source=Microsoft.AspNetCore.ApiAuthorization.IdentityServer
  StackTrace:
   at Microsoft.AspNetCore.ApiAuthorization.IdentityServer.ConfigureSigningCredentials.LoadKey()
   at …
Run Code Online (Sandbox Code Playgroud)

.net-core asp.net-core identityserver4 asp.net-core-identity asp.net-core-3.0

12
推荐指数
1
解决办法
6603
查看次数

Blazor Web Assembly App .Net Core Hosted:发布运行时错误

我使用 .NET 5.0 RC1 Framework 创建了一个托管在 .NET Core 应用程序中的简单 Blazor Web 程序集项目,并在 Visual Studio 2019 预览版的向导期间添加了授权。它在调试中完美运行,但如果我发布它并尝试连接到服务器,我会收到 500 错误。检查日志错误是:

Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HM37MQFV38CV", Request id "0HM37MQFV38CV:00000003": An unhandled exception was thrown by the application.
      System.NullReferenceException: Object reference not set to an instance of an object.
         at Microsoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions.<>c.<AddSigningCredentials>b__10_2(IServiceProvider sp)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, RuntimeResolverContext context)
         at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitNoCache(ServiceCallSite callSite, TArgument …
Run Code Online (Sandbox Code Playgroud)

blazor visual-studio-2019 .net-5 blazor-webassembly blazor-hosted

9
推荐指数
2
解决办法
3070
查看次数

我们如何创建非对称安全密钥?

我们如何在 C# 中创建 AsymmetrySecurityKey。实际上,我们正在使用 AsymetricSecurityKey 创建签名凭据,这是我们的代码:

// Define const Key this should be private secret key  stored in some safe place
string key = "401b09eab3c013d4ca54922bb802bec8fd5318192b0a75f201d8b3727429090fb337591abd3e44453b954555b7a0812e1081c39b740293f765eae731f5a65ed1";

// Create Security key  using private key above:
// not that latest version of JWT using Microsoft namespace instead of System
var securityKey = new AsymmetricSecurityKey(Encoding.UTF8.GetBytes(key));

// Also note that securityKey length should be >256b
// so you have to make sure that your private key has a proper length
//
var credentials = new …
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
1
解决办法
1万
查看次数

Blazor - app.UseIdentityServer(); 使用 .pfx 密钥文件 - 解析数字时遇到意外字符

我创建了一个新的 Blazor WebAssembly 应用程序,其中包含个人用户帐户、应用程序内存储用户帐户和 .NET 5 中托管的 ASP.NET Core。将我的应用程序部署到 Azure 应用服务时,出现以下错误:

\n
\n

未将对象引用设置为对象的实例。at\nMicrosoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions

\n
\n

阅读这些链接我必须在生产中为 IdentityServer 提供我自己的证书:

\n

Blazor Web Assembly 应用程序 .Net Core 托管:发布运行时错误

\n

/sf/answers/3983280031/

\n

https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-api-authorization?view=aspnetcore-5.0#example-deploy-to-azure-app-service

\n

然后我创建了一个.pfx这样的文件,并且我已经验证它是否有效并且我的密码是正确的。

\n

/sf/answers/3415306191/

\n

然后,我将该.pfx文件放置在我的Server项目根文件夹中并标记Copy to Output Directory为Copy Always.

\n

然后我更新appsettings.json为如下所示:

\n
  "IdentityServer": {\n    "Clients": {\n      "BlazorTest.Client": {\n        "Profile": "IdentityServerSPA"\n      }\n    },\n    "Key": {\n      "Type": "File",\n      "FilePath": "localhost.pfx",\n      "Password": "MySercurePassword123?"\n    }\n  },\n
Run Code Online (Sandbox Code Playgroud)\n

现在该项目既无法在本地运行,也无法在 Azure 上运行。它失败并 …

c# azure identityserver4 blazor blazor-webassembly

5
推荐指数
1
解决办法
1546
查看次数

WindowsCryptographicException:密钥集不存在

我想创建或获取证书,创建pfx文件,加载它并让IdentityServer使用它。但是,IdentityServer无法使用我的pfx。

如何成功创建pfx并让IdentityServer使用它?

我有一个使用ASP.Net Core在localhost上运行的IdentityServer4 Web解决方案。我正在为在Azure上运行的生产准备解决方案,并将证书添加到IdentityServer管道中,而不是开发人员签名证书中。

通常这意味着交换代码,例如

services.AddIdentityServer()
        .AddDeveloperSigningCredential()
Run Code Online (Sandbox Code Playgroud)

与

services.AddIdentityServer()
        .AddSigningCredential(cert)
Run Code Online (Sandbox Code Playgroud)

其中,“ cert”是X509Certificate2的对象实例,该对象实例已加载包含证书(带有公钥和私钥)的PFX文件。

我正在使用的证书是自签名的,并且是按照Ben Cull博客https://benjii.me/2017/06/creating-self-signed-certificate-identity-server-azure/上的步骤创建的

证书和私钥将导出到PFX文件,以便将公钥和私钥都包含在文件中。

PFX文件的加载成功。加载的代码是

cert = new X509Certificate2(Path.Combine(Environment.ContentRootPath, "MyPfxFile.pfx"), "password-used-when-exporting-pfx");
Run Code Online (Sandbox Code Playgroud)

Pfx文件位于Web项目内,并且已找到并读入X509Certificate对象。

在Visual Studio中检查X509Certificate2时,属性HasPrivateKey返回true,但是PrivateKey属性解析为观察程序中的异常。

'cert.PrivateKey' threw an exception of type 'Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException'
Run Code Online (Sandbox Code Playgroud)

所有其他属性在观察者中看起来都很好。

另外,当我在证书对象实例的“即时窗口”方法中执行GetRSAPrivateKey时,它将返回异常。

cert.GetRSAPrivateKey()
'cert.GetRSAPrivateKey()' threw an exception of type 'Internal.Cryptography.CryptoThrowHelper.WindowsCryptographicException'
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2146893798
    HelpLink: null
    InnerException: null
    Message: "Keyset as registered is invalid"
    Source: "System.Security.Cryptography.Csp"
    StackTrace: "   at Internal.NativeCrypto.CapiHelper.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)\r\n   at System.Security.Cryptography.RSACryptoServiceProvider.get_SafeProvHandle()\r\n   at System.Security.Cryptography.RSACryptoServiceProvider.get_SafeKeyHandle()\r\n   at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 keySize, CspParameters parameters, Boolean useDefaultKeySize)\r\n   at …
Run Code Online (Sandbox Code Playgroud)

x509certificate asp.net-core identityserver4

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