winform + wcf (netTcpBinding) 양 방향 통신 사용자 정의 사용자 이름 암호 검증

인증서 만 들 기     
       vs 명령 으로 만 들 기
        makecert.exe -sr LocalMachine -ss My -a sha1 -n CN=TestServer -sky exchange -pe
      인증서 만 들 기 세부 정보http://blog.csdn.net/aojiancc2/article/details/17711925      
wcf 서버 쪽
       
namespace WcfCertificate
{
    //   :   “  ”    “   ”  ,                  “ITcpCer”。
     [ServiceContract(CallbackContract = typeof(ICallback))]//  UserCallBack    
    public interface ITcpCer
    {
        [OperationContract]
        void DoWork();
    }

    public interface ICallback  //         
    {

        [OperationContract(IsOneWay = true)]//    ,      
        void TestCallBack(string hello);
    }
}
namespace WcfCertificate
{
    //   :   “  ”    “   ”  ,        、svc          “TcpCer”。
    //   :      WCF            ,               TcpCer.svc   TcpCer.svc.cs,      。
    public class TcpCer : ITcpCer
    {
        public void DoWork()
        {

            OperationContext.Current.GetCallbackChannel<ICallback>().TestCallBack("   ");
        }
    }
}

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>

  <system.serviceModel>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="mybehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="None" />
            </clientCertificate>
            <serviceCertificate findValue="TestServer" storeLocation="LocalMachine"
              storeName="My" x509FindType="FindBySubjectName" />
            <userNameAuthentication userNamePasswordValidationMode="Custom"
              customUserNamePasswordValidatorType="WcfCertificate.Validator,WcfCertificate" />
          </serviceCredentials>
        </behavior>

      </serviceBehaviors>
    </behaviors>

     <bindings>

       <netTcpBinding>
         <binding name="netTcpBindConfig" receiveTimeout="00:20:00">
           <security mode="Message">
             <message clientCredentialType="UserName"  />
           </security>
         </binding>
       </netTcpBinding>
       
    </bindings>
    <services>

      <service name="WcfCertificate.TcpCer" behaviorConfiguration="mybehavior">
        <endpoint address="" binding="netTcpBinding"  bindingConfiguration="netTcpBindConfig" contract="WcfCertificate.ITcpCer"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    
    </services>
    
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
                   Web        ,          True。
                    False       Web          。
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

고객 센터
namespace wfTcpCer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ServiceReference1.TcpCerClient tc = new ServiceReference1.TcpCerClient(
                new System.ServiceModel.InstanceContext(new CallBack()));
            tc.ClientCredentials.UserName.UserName = "sa";
            tc.ClientCredentials.UserName.Password = "1234";
            tc.DoWorkAsync();//            
        }

        public class CallBack : ServiceReference1.ITcpCerCallback //    
        {
            public void TestCallBack(string hello) 
            {
                MessageBox.Show("text callback");
            }
        }
    }
}

프로필
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_ITcpCer">
                    <security mode="Message">
                        <message clientCredentialType="UserName" />
                    </security>
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://2011-20130817ae:4503/TcpCer.svc"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ITcpCer"
                contract="ServiceReference1.ITcpCer" name="NetTcpBinding_ITcpCer">
                <identity>
                    <!--        ,       dns value="TestServer"  certificateValidationMode="None"       -->
                    <certificate encodedValue="AwAAAAEAAAAUAAAA3j+VKPLILWzCfoG9VXc2yhchi+kgAAAAAQAAADoCAAAwggI2MIIB5KADAgECAhDmLEMHisejr0j4qDS+rn+HMAkGBSsOAwIdBQAwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3kwHhcNMTMxMjMxMDM0MjE4WhcNMzkxMjMxMjM1OTU5WjAVMRMwEQYDVQQDEwpUZXN0U2VydmVyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtXJzwFZrUgBRsLu5c8wt0iEwj/EcI9IFHK8tAraRtKsLtoWi3CHxrwi39rNaWh0SN11w8QX+0rEwAdxoxppGPPTTMbRMlcUHWHZ8oTn5V66nzYds0X2tt993KV0k1YX1ZXDuA37LZFROwksqCYfsl2sAenVkiLizzjrMVaIUPuzVvaphpxd+KcivPAYdNW/tCgC1vracULEVnu+86uPhyT9dUykIByB4rhbR0EHUNuw/4CtTsx+a3Wqn/vhPgjy08nxx+wfB/ekJmNnoz0cNVobq4WOpExDNZdGrrow9qVMcIvcGj53vGb4BhzdCwZWzf0U/K3LAJEqg+2/P+HlcPwIDAQABo0swSTBHBgNVHQEEQDA+gBAS5AktBh0dTwCNYSHcFmRjoRgwFjEUMBIGA1UEAxMLUm9vdCBBZ2VuY3mCEAY3bACqAGSKEc+41KpcNfQwCQYFKw4DAh0FAANBABjcWxmn9cVILyozv4cVvGQJ6ZfAen7pHI1MV1dZ/YhJHd1Ou6dHHafAI3755TyY7gpEHPu4xuRN6eXwYg2FkvI=" />
                    <!--<dns value="TestServer"/>-->
                </identity>
            </endpoint>
        </client>

      <behaviors>
        <endpointBehaviors>
          <behavior name="mye">
            <clientCredentials>
              <serviceCertificate>
                <!--        None-->
                <authentication certificateValidationMode="None"/>
              </serviceCertificate>
            </clientCredentials>
          </behavior>
        </endpointBehaviors>
      </behaviors>
      
    </system.serviceModel>
</configuration>

좋은 웹페이지 즐겨찾기