2017年7月8日土曜日

WCFでhttps通信 クライアント(exe)

自己署名入り証明書の場合は、以下のプログラムで。

[引用]
http://www.atmarkit.co.jp/fdotnet/dotnettips/867sslavoidverify/sslavoidverify.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Security;

using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;

namespace WCFTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            ServicePointManager.ServerCertificateValidationCallback =  new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var client = new ServiceReference1.Service1Client();
            var s = client.GetData(10);
            MessageBox.Show(s);
            client.Close();
        }

        private bool OnRemoteCertificateValidationCallback(
                                                          Object sender,
                                                          X509Certificate certificate,
                                                          X509Chain chain,
                                                          SslPolicyErrors sslPolicyErrors)
        {
            return true;  // 「SSL証明書の使用は問題なし」と示す
        }

    }
}

WCFでhttps通信[App.config]

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpsBinding>
                <binding name="SecureBinding" />
            </basicHttpsBinding>
        </bindings>
        <client>
            <endpoint address="https://[servername]/Test/Service1.svc" binding="basicHttpsBinding"
                bindingConfiguration="SecureBinding" contract="ServiceReference1.IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

WCFでhttps通信[web.config]

 ■VisualStudio2013だとprotocolMappingが設定されなかったので自分で追加しないといけなかった。
■VisualStudio2017ではprotocolMappingは自動で追加された。

protocolMappingでhttpsがないと、エンドポイントがアクティブ化できないとエラーになる。
このweb.configは適当に変更すること。

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.2" />
    <httpRuntime targetFramework="4.6.2"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="Service1.svc" binding="basicHttpsBinding" bindingConfiguration="SecureBinding"
          contract="WcfService1.IService1" />
        <host>
          <baseAddresses>
            <add baseAddress="https://[servername]/test" />
          </baseAddresses>

        </host>
      </service>
    </services>
    <bindings>
      <basicHttpsBinding>
        <binding name="SecureBinding" />
      </basicHttpsBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- メタデータ情報の開示を避けるには、展開する前に下の値を false に設定します -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- デバッグ目的で障害発生時の例外の詳細を受け取るには、下の値を true に設定します。例外情報の開示を避けるには、展開する前に false に設定します -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add scheme="https" binding="basicHttpsBinding" bindingConfiguration="SecureBinding" />
    </protocolMapping>   

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        デバッグ中に Web アプリケーションのルートを直接参照するには、下の値を true に設定します。
        Web アプリケーション フォルダーの情報開示を避けるには、展開する前に false に設定します。
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>