CreateChannel with ClientCredentials属性

时间:2014-02-19 11:11:44

标签: c# wcf client-certificates contract

我对WCF很新,并尝试使用自定义用户名和密码创建WCF服务。我知道我应该将userName和Password设置为代理的ClientCredentials,但出于某种原因,我没有这样的属性......

我认为它与我的合同有关,所以这里是:

我的合约代码:

namespace Contracts
{    
    [ServiceContract]
    public interface ICalc
    {
        [OperationContract]
        CalcResponse Add(double x, double y);

        [OperationContract]
        CalcResponse Substract(double x, double y);

        [OperationContract]
        CalcResponse Multiply(double x, double y);

        [OperationContract]
        CalcResponse Divide(double x, double y);
    }
}

在我的客户端,我所要做的就是:

ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
ICalc proxy = channel.CreateChannel();

proxy.ClientCredentials.UserName.UserName = "USER";
proxy.ClientCredentials.UserName.Password = "PASSWORD";

但我的代理没有ClientCredentials属性

更新 我遇到了一些导致其他错误的问题。当我解决它们时,我遇到了一个新错误:

  

套接字连接已中止。这可能是由错误引起的   处理您的消息或超过接收超时   远程主机或底层网络资源问题。

我的超时在客户端和服务器中都设置为5分钟。我在不到一分钟的时间内得到了这个错误......

这是我更新的代码:

ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");

var defaultCredentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
channel.Endpoint.Behaviors.Remove(defaultCredentials);

ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "Comply";
loginCredentials.UserName.Password = "123456";

channel.Endpoint.Behaviors.Add(loginCredentials); 
ICalc proxy = channel.CreateChannel();

我有一个自定义验证器,我在那里放了一个断点,但它只是没有在那里... 我的验证码:

class CustomUserNameValidator : UserNamePasswordValidator
{
    public override void Validate(string userName, string password)
    {
        if (userName == "comply" && password == "123456")
        {
        }
    }
}

配置(在客户端和服务器上):

<service name ="WcfServiceLibrary.Service1" behaviorConfiguration ="CustomValidator">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/CalcIISHost/mex"/>
        <add baseAddress ="net.tcp://localhost:808/CalcIISHost/CalcService"/>
      </baseAddresses>
    </host>
    <endpoint address ="" binding ="netTcpBinding" bindingConfiguration="tcpWithMessageSecurity" contract ="Contracts.ICalc"></endpoint>
    <endpoint address ="net.tcp://localhost:5001/CalcIISHost/mex" binding ="mexTcpBinding" contract ="IMetadataExchange"></endpoint>
  </service>
...
<behavior name="CustomValidator">
      <serviceCredentials>
        <userNameAuthentication
          userNamePasswordValidationMode="Custom"
          customUserNamePasswordValidatorType="WCFClasses.CustomUserNameValidator, WCFClasses"/>
        <serviceCertificate
          findValue="localhost"
          x509FindType="FindBySubjectName"
          storeLocation="CurrentUser"
          storeName="My" />
      </serviceCredentials>
      <serviceMetadata httpGetEnabled ="true"/>
    </behavior>
...
<netTcpBinding>
    <binding name="tcpWithMessageSecurity"  sendTimeout="00:05:00" receiveTimeout="00:05:00">
      <security mode="Message" >
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
  </netTcpBinding>

任何想法我做错了什么?

1 个答案:

答案 0 :(得分:4)

您可以像这样设置凭据......

删除默认端点行为

ChannelFactory<ICalc> channel = new ChannelFactory<ICalc>("calcEndpoint");
var defaultCredentials = channel.Endpoint.Behaviors.Find<ClientCredentials>();
channel.Endpoint.Behaviors.Remove(defaultCredentials); 

创建凭据

ClientCredentials loginCredentials = new ClientCredentials();
loginCredentials.UserName.UserName = "USER";
loginCredentials.UserName.Password = "PASSWORD";

将凭据设置为出厂时的新端点行为

channel.Endpoint.Behaviors.Add(loginCredentials); 
ICalc proxy = channel.CreateChannel();

编辑2月27日

我建议您向服务添加日志记录,并在 Error.svclog 文件中发布您遇到的异常,在服务app.config中的Configuration标记下添加。

<system.diagnostics>
        <sources>
            <source name="System.ServiceModel"
                    switchValue="Information, ActivityTracing"
                    propagateActivity="true" >
                <listeners>
                    <add name="xml"/>
                </listeners>
            </source>
            <source name="System.ServiceModel.MessageLogging">
                <listeners>
                    <add name="xml"/>
                </listeners>
            </source>
            <source name="myUserTraceSource"
                    switchValue="Information, ActivityTracing">
                <listeners>
                    <add name="xml"/>
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add name="xml"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="Error.svclog" />
        </sharedListeners>
    </system.diagnostics>