2014-02-19 22 views
2

我对WCF很陌生,试图用自定义用户名和密码创建WCF服务。我知道我应该将userName和Password设置为代理的ClientCredentials,但由于某种原因,我没有这样的属性...CreateChannel和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"; 

但我的代理doen't有ClientCredentials财产

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

The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.

我的超时设置为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> 

任何想法我做错了什么?

回答

3

您可以设置这样的凭证......

删除默认终结点行为

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(); 

EDIT 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> 
+0

谢谢!它似乎工作! :) –

+0

哦,恐怕它没有实际工作...我现在得到一个错误更新的问题... –

+0

我相信错误消息是不相关的传递凭据,我建议你跟踪错误信息。 –