Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 浏览器外部Asp.net Core Kestrel服务器HTTPS配置错误VS only:PR_END_OF_FILE_错误_C#_Asp.net_Asp.net Core_Https_Self Signed - Fatal编程技术网

C# 浏览器外部Asp.net Core Kestrel服务器HTTPS配置错误VS only:PR_END_OF_FILE_错误

C# 浏览器外部Asp.net Core Kestrel服务器HTTPS配置错误VS only:PR_END_OF_FILE_错误,c#,asp.net,asp.net-core,https,self-signed,C#,Asp.net,Asp.net Core,Https,Self Signed,我正在使用自签名证书(存储在LocalUser\Personal下)允许HTTPS连接到我的asp.net core Kestrel服务器,该服务器承载着我的web应用程序,Kestrel服务器在本地windows服务中运行。我使用c#生成此证书,但在单独的代码中。我的问题是,当从VisualStudio内部运行web应用程序时,一切正常,但是当将应用程序作为windows服务(从VisualStudio外部)运行时,我无法访问我的网页。我的firefox浏览器显示“连接到本地主机时出错:432

我正在使用自签名证书(存储在LocalUser\Personal下)允许HTTPS连接到我的asp.net core Kestrel服务器,该服务器承载着我的web应用程序,Kestrel服务器在本地windows服务中运行。我使用c#生成此证书,但在单独的代码中。我的问题是,当从VisualStudio内部运行web应用程序时,一切正常,但是当将应用程序作为windows服务(从VisualStudio外部)运行时,我无法访问我的网页。我的firefox浏览器显示“连接到本地主机时出错:43232.PR\u END\u OF\u FILE\u error”

这是我持有私钥的证书:

下面是我如何从其存储区加载此证书:

private static void StartWebAPI()
{
    ExtendedLogHelper.LogVerboseInformation(LogingActivityColeration.SERVICE_CYCLE, "Starting client web API.");

    try
    {
        var appSettings = new ConfigurationBuilder()
            .SetBasePath(CycleData.ApplicationDirectory)
            .AddJsonFile("appsettings.json", optional: true).Build();

        CurrentDirectoryHelpers.SetCurrentDirectory();
        Host.CreateDefaultBuilder(null)
           .ConfigureWebHostDefaults(webBuilder =>
           {
               webBuilder.UseKestrel(ksOptions =>
               {
                   appSettings.GetSection("Urls").Value?.Split(";")?.ToList().ForEach(url =>
                   {
                       ExtendedLogHelper.LogVerboseInformation($"Configuring server to listen on the following url {url}");
                       Uri uri = new Uri(url);

                       if(uri.Scheme.Trim().Equals("https",StringComparison.InvariantCultureIgnoreCase))
                           ksOptions.ListenLocalhost(uri.Port, conf => conf.UseHttps(ImportKestrelCertificate()));
                       else
                           ksOptions.ListenLocalhost(uri.Port);
                   });
                   
               });
               webBuilder.UseStartup<APIStartup>();
           }).ConfigureServices(services =>
           {
               services.AddHostedService<EndpointLauncher>();
           }).UseWindowsService().Build().Run();
    }
    catch (Exception e)
    {
        ExtendedLogHelper.LogError(e);
    }
}
internal static X509Certificate2 ImportKestrelCertificate()
{
    X509Certificate2 x509Certificate2 = null;
    ExtendedLogHelper.LogVerboseInformation("Loading server certificate...");
    try
    {
        string subjectName = "localhost";
        var userCertificates = X509Certificate2Generator.GetCertificates(StoreName.My, StoreLocation.LocalMachine);
        userCertificates.ForEach(cert => ExtendedLogHelper.LogVerboseInformation(cert.Subject));
        x509Certificate2 = userCertificates.Find(x => x.Subject.Contains("CN=" + subjectName, StringComparison.InvariantCultureIgnoreCase));
        if (x509Certificate2 == null)
        {
            ExtendedLogHelper.LogVerboseInformation("Server certificate not found, generating new self-signed certificate.");
            var result = X509Certificate2Generator.GenerateSelfSignedCertificateCA("localhost", subjectName);
            x509Certificate2 = result.Item2;

            if (x509Certificate2 == null)
                throw new Exception($"Failed to load or generate the Https required certificate.");

            X509Certificate2Generator.addCertToStore(result.Item1, StoreName.Root, StoreLocation.LocalMachine);
            X509Certificate2Generator.addCertToStore(x509Certificate2, StoreName.My, StoreLocation.LocalMachine);
        }

        return x509Certificate2;
    }
    catch (Exception ex)
    {
        throw new Exception("Server certificate loading failed.", ex);
    }
}
private static void StartWebAPI()
{
ExtendedLogHelper.LogVerboseInformation(LogingActivityColeration.SERVICE_CYCLE,“启动客户端web API”);
尝试
{
var appSettings=new ConfigurationBuilder()
.SetBasePath(CycleData.ApplicationDirectory)
.AddJsonFile(“appsettings.json”,可选:true).Build();
CurrentDirectoryHelpers.SetCurrentDirectory();
Host.CreateDefaultBuilder(空)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseKestrel(ksOptions=>
{
appSettings.GetSection(“url”).Value?.Split(;”).ToList().ForEach(url=>
{
ExtendedLogHelper.LogVerboseInformation($“将服务器配置为侦听以下url{url}”);
Uri=新的Uri(url);
if(uri.Scheme.Trim().Equals(“https”,StringComparison.InvariantCultureIgnoreCase))
ksOptions.ListenLocalhost(uri.Port,conf=>conf.UseHttps(ImportKestrelCertificate());
其他的
ksOptions.ListenLocalhost(uri.Port);
});
});
webBuilder.UseStartup();
}).ConfigureServices(服务=>
{
services.AddHostedService();
}).UseWindowsService().Build().Run();
}
捕获(例外e)
{
扩展loghelper.LogError(e);
}
}
内部静态X509Certificate2导入KESTRELCertificate()
{
X509Certificate2 X509Certificate2=null;
LogVerboseInformation(“正在加载服务器证书…”);
尝试
{
字符串subjectName=“localhost”;
var userCertificates=X509Certificate2Generator.GetCertificates(StoreName.My,StoreLocation.LocalMachine);
ForEach(cert=>ExtendedLogHelper.LogVerboseInformation(cert.Subject));
x509Certificate2=userCertificates.Find(x=>x.Subject.Contains(“CN=“+subjectName,StringComparison.InvariantCultureIgnoreCase));
如果(x509Certificate2==null)
{
ExtendedLogHelper.LogVerboseInformation(“未找到服务器证书,正在生成新的自签名证书”);
var结果=X509Certificate2Generator.GenerateSelfSignedCertificateCA(“本地主机”,subjectName);
x509Certificate2=结果项2;
如果(x509Certificate2==null)
抛出新异常($“未能加载或生成Https所需的证书。”);
X509Certificate2Generator.addCertToStore(result.Item1,StoreName.Root,StoreLocation.LocalMachine);
X509Certificate2Generator.addCertToStore(x509Certificate2,StoreName.My,StoreLocation.LocalMachine);
}
返回x509Certificate2;
}
捕获(例外情况除外)
{
抛出新异常(“服务器证书加载失败”,例如);
}
}
有什么想法吗?先谢谢你