热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在WCF客户端中接受自签名SSL证书?-Howtoacceptaself-signedSSLcertificateinaWCFclient?

ThismaybeastupidquestionbutIjustcantfindtheanswer.这可能是一个愚蠢的问题,但我找不到答案。WhatIwouldl

This may be a stupid question but I just can't find the answer.

这可能是一个愚蠢的问题,但我找不到答案。

What I would like to do: I have a WCF service hosted by IIS. It is working perfectly, I can access the wsdl, I have a self-signed certificate for the server etc. I would like to call this service from a WPF client.

我想做什么:我有一个由IIS托管的WCF服务。它工作得很好,我可以访问wsdl,我有一个服务器的自签名证书等。我想从WPF客户端调用此服务。

The problem is, since I have a self-signed certificate, I get the following exception when calling the service: Could not establish trust relationship for the SSL/TLS secure channel with authority 'localhost'.

问题是,由于我有自签名证书,因此在调用服务时出现以下异常:无法为具有权限“localhost”的SSL / TLS安全通道建立信任关系。

If I access the site (or the service) from a browser, it is no problem, because the browser warns me about the certificate, and gives me the choice of viewing the page anyway. But the WPF client just throws an exception.

如果我从浏览器访问该站点(或服务),则没有问题,因为浏览器会向我发出关于证书的警告,并且让我可以选择查看该页面。但WPF客户端只是抛出异常。

I don't want to completely turn off the authentication process, I simply would like to give the users the option of ignoring this warning (as browsers do).

我不想完全关闭身份验证过程,我只是想让用户选择忽略此警告(就像浏览器那样)。

Can anyone provide some code for this? If you ran into a good, detailed tutorial about this, it would be awesome too. (See, my problem with the tutorials I've found is the lack of details)

任何人都可以为此提供一些代码吗?如果你遇到一个关于这个的好的,详细的教程,它也会很棒。 (看,我发现的教程问题是缺乏细节)

2 个解决方案

#1


6  

You can register the certificate yourself. If load the certificate in the client as well, and then register the it as trusted you shouldn't get that warning.

您可以自己注册证书。如果在客户端中加载证书,然后将其注册为可信任,则不应该收到该警告。

You need to find a X509CertificateCollection and add the certificate to that collection. I had this kind of problem with a SmtpClient running over Ssl.

您需要找到X509CertificateCollection并将证书添加到该集合。我遇到了一个在Ssl上运行的SmtpClient的问题。

By hooking the System.Net.ServicePointManager.ServerCertificateValidationCallback or implementing System.Net.ICertificatePolicy and identify my own installed certificate as valid/trusted (attached to the System.Net.ServicePointManager.CertificatePolicy).

通过挂钩System.Net.ServicePointManager.ServerCertificateValidationCallback或实现System.Net.ICertificatePolicy并将我自己安装的证书标识为有效/可信(附加到System.Net.ServicePointManager.CertificatePolicy)。

This is not WCF stuff per se, but from what I could tell, this should translate to WCF as well. It all depends what WCF is uses under the hood.

这本身不是WCF的东西,但从我所知道的,这也应该转换为WCF。这完全取决于WCF在引擎盖下的用途。

#2


13  

Here's the minimum amount of code you need to make WCF client accept an arbitrary certificate. This is not secure. Use for testing only. Don't blame me if this code goes berserk and eats your little kitten.

这是使WCF客户端接受任意证书所需的最少代码量。这不安全。仅用于测试。如果这个代码发狂并吃掉你的小猫,不要怪我。

ServicePointManager.ServerCertificateValidationCallback +=
            new System.Net.Security.RemoteCertificateValidationCallback(EasyCertCheck);

The call back:

回电:

bool EasyCertCheck(object sender, X509Certificate cert,
        X509Chain chain, System.Net.Security.SslPolicyErrors error)
{
    return true;
}

Code shamelessly lifted from the least helpful answer to Is it possible to force the WCF test client to accept a self-signed certificate?

代码无耻地从最不实用的答案解除了是否可以强制WCF测试客户端接受自签名证书?


推荐阅读
  • 引号快捷键_首选项和设置——自定义快捷键
    3.3自定义快捷键(CustomizingHotkeys)ChemDraw快捷键由一个XML文件定义,我们可以根据自己的需要, ... [详细]
  • 【技术分享】一个 ELF 蠕虫分析
    【技术分享】一个 ELF 蠕虫分析 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 浅解XXE与Portswigger Web Sec
    XXE与PortswiggerWebSec​相关链接:​博客园​安全脉搏​FreeBuf​XML的全称为XML外部实体注入,在学习的过程中发现有回显的XXE并不多,而 ... [详细]
  • KVC:Key-valuecodingisamechanismforindirectlyaccessinganobject’sattributesandrelations ... [详细]
  • C++程序员视角下的Rust语言
    自上世纪80年代初问世以来,C就是一门非常重要的系统级编程语言。到目前为止,仍然在很多注重性能、实时性、偏硬件等领域发挥着重要的作用。C和C一样&#x ... [详细]
  • 第七章•Firewalld防火墙实战
    1、防火墙安全基本概述在CentOS7系统中集成了多款防火墙管理工具,默认启用的是firewalld(动态防火墙管理器)防火墙管理工具,Firewalld支持CLI(命令行)以及G ... [详细]
  • angular2/4 使用[innerHTML]时样式不生效
    首先默认情况下,innerHTML会被认为是不安全的,因此屏蔽掉了样式。不生效场景我们通过innerHTML引入一段html格式的文本, ... [详细]
  • Forexperiencedcryptoinvestors,thereareseveralsectorsthatseemedpromisingbutdidn’tlive ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • centos安装Mysql的方法及步骤详解
    本文介绍了centos安装Mysql的两种方式:rpm方式和绿色方式安装,详细介绍了安装所需的软件包以及安装过程中的注意事项,包括检查是否安装成功的方法。通过本文,读者可以了解到在centos系统上如何正确安装Mysql。 ... [详细]
  • 本文介绍了如何在Azure应用服务实例上获取.NetCore 3.0+的支持。作者分享了自己在将代码升级为使用.NET Core 3.0时遇到的问题,并提供了解决方法。文章还介绍了在部署过程中使用Kudu构建的方法,并指出了可能出现的错误。此外,还介绍了开发者应用服务计划和免费产品应用服务计划在不同地区的运行情况。最后,文章指出了当前的.NET SDK不支持目标为.NET Core 3.0的问题,并提供了解决方案。 ... [详细]
  • 本文介绍了在go语言中利用(*interface{})(nil)传递参数类型的原理及应用。通过分析Martini框架中的injector类型的声明,解释了values映射表的作用以及parent Injector的含义。同时,讨论了该技术在实际开发中的应用场景。 ... [详细]
author-avatar
qiao203
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有