【问题标题】:CreateChannel with ClientCredentials property具有 ClientCredentials 属性的 CreateChannel
【发布时间】:2014-02-19 11:11:43
【问题描述】:

我对 WCF 还很陌生,我正在尝试使用自定义用户名和密码创建 WCF 服务。我知道我应该将用户名和密码设置为代理的 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 设置为 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>

任何想法我做错了什么?

【问题讨论】:

    标签: c# wcf client-certificates contract


    【解决方案1】:

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

    移除默认端点行为

    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 中的配置标记下添加以下内容。

    <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>
    

    【讨论】:

    • 哦,恐怕它实际上并没有工作......我现在收到问题中更新的错误......
    • 我认为错误信息与传递凭据无关,建议您跟踪错误信息。
    • 我有...没有错误...只有传输和信息。和一些活动边界(开始和停止),如果我做对了 - 这并不意味着真的有什么问题......
    • 我已经编辑了我的答案,将诊断配置添加到您的服务配置中并让我知道异常
    • 仍然一无所获...在那两天里,我稍微更改了我的代码...经过多次尝试后,我让它工作了,但是我的验证器被跳过了...当我更改配置时因此将调用验证器 - 我再次遇到错误(“套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或底层网络资源问题”) .即使我的超时设置为 5 分钟,但我在 1 秒后得到了这个异常......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多