gpt4 book ai didi

c# - 无法使用授权 wcf C# wcf rest 服务为 ssl/tls 建立安全通道

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:21 25 4
gpt4 key购买 nike

我创建了一个客户端应用程序,它从我的其余 wcf 服务获取数据,如您所见:

 Uri reqUri = new Uri("https://localhost/paymentservice.svc/listpayment");



WebRequest req = WebRequest.Create(reqUri);

req.PreAuthenticate = true;

NetworkCredential credential = new NetworkCredential("test", "test123");

req.Credentials = credential;

WebResponse resp = req.GetResponse();


DataContractSerializer data = new DataContractSerializer(typeof(string));
var res = data.ReadObject(resp.GetResponseStream());

Console.WriteLine(res);

Console.ReadLine();

如您所见,我在 iis 中创建了一个证书:

enter image description here

并将我发布的文件上传到上面。但是当我调用我的客户时,我得到了这个错误:

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

Additional information: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

这是我的服务网络配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>
<authentication mode="None" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="secureHttpBinding">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="Payment.Application.ServiceImplement.PaymentService" behaviorConfiguration="customBehaviour">
<endpoint address=""
binding="webHttpBinding"
contract="Payment.Domain.Service.IPaymentService"
behaviorConfiguration="web"/>

</service>
<service name="Payment.Infrustructure.RepositoryImplement.PaymentRepository" behaviorConfiguration="customBehaviour" >
<endpoint address=""
binding="webHttpBinding"
contract="Payment.Domain.Repository.IPaymentRepository"
behaviorConfiguration="web"/>
</service>

</services>

<behaviors>
<serviceBehaviors>
<behavior name="customBehaviour">
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Payment.Service.UserAuthentication,Payment.Service"/>
</serviceCredentials>

<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>

<behavior name="web">
<webHttp/>

</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Methods" value="GET, POST,PUT,DELETE" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking" />
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;initial catalog=SymfaDB;user id= sa ;password=12345;" providerName="System.Data.SqlClient" />
<!--<add name="DefaultConnection" connectionString="Data Source=92.50.12.222,1433;initial catalog=ParkingDB;user id= sa ;password=123qweQWE@;" providerName="System.Data.SqlClient" />-->
</connectionStrings>
</configuration>

当我在 visual studio 中运行项目并调用此 url http://localhost:4428/PaymentService.svc/listpayment 我得到数据,如您所见: enter image description here

但是当我将发布文件上传到 iis 并调用此 url https://localhost/PaymentService.svc/listpayment 时,如您所见,我收到此错误:

enter image description here

如您所见,当我调用此 https://localhost/PaymentService.svc 时,我的服务可用。 enter image description here

最佳答案

您需要将证书安装为受信任的来源。

  1. 打开具有管理员权限的命令提示符,键入“mmc”并按回车键,这将打开 Microsoft 管理控制台。
  2. 从菜单转到文件 > 添加/删除管理单元,选择证书并单击添加
  3. 选择计算机帐户并单击下一步,选择本地计算机并单击完成。
  4. 转到证书(本地计算机)> 个人 > 证书
  5. 从菜单转到操作 > 所有任务 > 导入
  6. 在证书导入向导中单击“下一步”,提供证书文件的路径,输入密码(如果有),然后单击“下一步”、“下一步”和“完成”。
  7. 现在您将返回到 Microsoft 管理控制台,单击“受信任的根证书颁发机构”,选择“证书”、“操作”>“所有任务”>“导入”,然后按照步骤 6 进行操作。

此外,URL 中使用的主机名应与证书上的名称相匹配。确保您使用的 URL 与证书“颁发给”字段中的 URL 相同。

关于c# - 无法使用授权 wcf C# wcf rest 服务为 ssl/tls 建立安全通道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44565099/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com