【问题标题】:how to add a certificate to WCF ChannelFactory?如何向 WCF ChannelFactory 添加证书?
【发布时间】:2011-01-05 15:25:41
【问题描述】:

我需要一个简单的 WCF 示例代码或教程或显示如何将证书添加到 ChannelFactory 的步骤的链接?

【问题讨论】:

    标签: wcf certificate channelfactory


    【解决方案1】:

    这有帮助吗?

         // Create a proxy with the previously create binding and 
               // endpoint address
                  channelFactory = 
                     new ChannelFactory<IEchoService>(
                         multipleTokensBinding, serviceAddress);
               // configure the username credentials, the client 
               // certificate and the server certificate on the channel 
               // factory 
               channelFactory.Credentials.UserName.UserName = username;
               channelFactory.Credentials.UserName.Password = password;
               channelFactory.Credentials.ClientCertificate.SetCertificate(
               "CN=client.com", StoreLocation.CurrentUser, StoreName.My);
                  channelFactory.Credentials.ServiceCertificate.SetDefaultCertificate(
               "CN=localhost", StoreLocation.LocalMachine, StoreName.My);
               client = channelFactory.CreateChannel();
               Console.WriteLine("Echo service returned: {0}", 
                                               client.Echo());
    
               ((IChannel)client).Close();
               channelFactory.Close();
            }
            catch (CommunicationException e)
            {
             Abort((IChannel)client, channelFactory);
             // if there is a fault then print it out
             FaultException fe = null;
             Exception tmp = e;
             while (tmp != null)
             {
                fe = tmp as FaultException;
                if (fe != null)
                {
                    break;
                }
                tmp = tmp.InnerException;
            }
            if (fe != null)
            {
               Console.WriteLine("The server sent back a fault: {0}", 
             fe.CreateMessageFault().Reason.GetMatchingTranslation().Text);
            }
            else
            {
             Console.WriteLine("The request failed with exception: {0}",e);
            }
        }
        catch (TimeoutException)
        {
            Abort((IChannel)client, channelFactory);
            Console.WriteLine("The request timed out");
        }
        catch (Exception e)
        {
             Abort((IChannel)client, channelFactory);
              Console.WriteLine(
              "The request failed with unexpected exception: {0}", e);
        }
        Console.WriteLine();
        Console.WriteLine("Press <ENTER> to terminate client.");
        Console.ReadLine();
    }
    

    【讨论】:

    • @感谢 Zedo,感谢您的回复。任何有用的链接来理解步骤/代码?我会同时尝试实现这一点。
    猜你喜欢
    • 2011-01-05
    • 2011-08-03
    • 2017-02-16
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    相关资源
    最近更新 更多