.net 3.5 不使用SDK连接到使用IFD配置的Dynamics CRM

.net 3.5 不使用SDK连接到使用IFD配置的Dynamics CRM,.net-3.5,dynamics-crm-2011,adfs2.0,.net 3.5,Dynamics Crm 2011,Adfs2.0,我们最近已将Dynamics CRM配置为在IFD中使用ADFS。我们正在尝试从.Net 3.5连接到它,因此无法使用CRM SDK。下面是我们在配置IFD之前使用的代码,它工作正常 HttpsTransportBindingElement httpTransport = new HttpsTransportBindingElement(); httpTransport.AuthenticationScheme = System.Net.AuthenticationS

我们最近已将Dynamics CRM配置为在IFD中使用ADFS。我们正在尝试从.Net 3.5连接到它,因此无法使用CRM SDK。下面是我们在配置IFD之前使用的代码,它工作正常

HttpsTransportBindingElement httpTransport = new HttpsTransportBindingElement();              
httpTransport.AuthenticationScheme = System.Net.AuthenticationSchemes.Ntlm;
httpTransport.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
httpTransport.MaxReceivedMessageSize = 1024 * 1024 * 1024;

SecurityBindingElement securityElement = SecurityBindingElement.CreateSspiNegotiationBindingElement(true);

TextMessageEncodingBindingElement textMessageEncoding = new TextMessageEncodingBindingElement();
textMessageEncoding.MaxReadPoolSize = 64;
textMessageEncoding.MaxWritePoolSize = 16;
textMessageEncoding.WriteEncoding = Encoding.UTF8;

CustomBinding customBinding = new CustomBinding(securityElement, textMessageEncoding, httpTransport);
customBinding.OpenTimeout = new TimeSpan(0, 0, 120);
customBinding.ReceiveTimeout = new TimeSpan(0, 0, 120);
customBinding.SendTimeout = new TimeSpan(0, 0, 120);

string remoteAddress = String.Empty;


remoteAddress = "https://" + ServiceUri + "/OrgName/XrmServices/2011/Organization.svc";

ChannelFactory<IOrganizationService> factory = new ChannelFactory<IOrganizationService>(customBinding, remoteAddress);

ClientCredentials loginCredentials = factory.Endpoint.Behaviors.Find<ClientCredentials>();
factory.Endpoint.Behaviors.Remove(loginCredentials);

// step two - instantiate your credentials
loginCredentials = new ClientCredentials();
loginCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, Domain);
factory.Endpoint.Behaviors.Add(loginCredentials);

IEnumerable<OperationDescription> operations = factory.Endpoint.Contract.Operations;


foreach (OperationDescription operation in operations)
{
    DataContractSerializerOperationBehavior dcsob = operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
    if (dcsob == null)
    {
        dcsob = new DataContractSerializerOperationBehavior(operation);
    }
    operation.Behaviors.Remove(dcsob);
    dcsob.MaxItemsInObjectGraph = 1012 * 1024 * 1024;
    operation.Behaviors.Add(dcsob);
}


_orgProxy = factory.CreateChannel();
HttpsTransportBindingElement httpTransport=新的HttpsTransportBindingElement();
httpTransport.AuthenticationScheme=System.Net.AuthenticationSchemes.Ntlm;
httpTransport.HostNameComparisonMode=HostNameComparisonMode.strong通配符;
httpTransport.MaxReceivedMessageSize=1024*1024*1024;
SecurityBindingElement securityElement=SecurityBindingElement.CreateSPineGotiationBindingElement(true);
TextMessageEncodingBindingElement textMessageEncoding=新的TextMessageEncodingBindingElement();
textMessageEncoding.MaxReadPoolSize=64;
textMessageEncoding.MaxWritePoolSize=16;
textMessageEncoding.WriteEncoding=Encoding.UTF8;
CustomBinding CustomBinding=新的CustomBinding(securityElement、textMessageEncoding、httpTransport);
customBinding.OpenTimeout=新的时间跨度(0,0,120);
customBinding.ReceiveTimeout=新的时间跨度(0,0,120);
customBinding.SendTimeout=新的时间跨度(0,0,120);
string remoteAddress=string.Empty;
remoteAddress=“https://“+ServiceUri+”/OrgName/XrmServices/2011/Organization.svc”;
ChannelFactory=新的ChannelFactory(customBinding,remoteAddress);
ClientCredentials loginCredentials=factory.Endpoint.Behaviors.Find();
factory.Endpoint.Behaviors.Remove(loginCredentials);
//第二步-实例化您的凭据
loginCredentials=newclientcredentials();
loginCredentials.Windows.clientcredentials=新系统.Net.NetworkCredential(用户名、密码、域);
factory.Endpoint.Behaviors.Add(loginCredentials);
IEnumerable operations=factory.Endpoint.Contract.operations;
foreach(操作中的操作描述操作)
{
DataContractSerializerOperationBehavior dcsob=operation.Behaviors.Find();
如果(dcsob==null)
{
dcsob=新的数据契约行为(操作);
}
操作.行为.删除(dcsob);
dcsob.MaxItemsInObjectGraph=1012*1024*1024;
操作.行为.添加(dcsob);
}
_orgProxy=factory.CreateChannel();
现在,当我们尝试使用此代码连接到CRM时,返回以下错误:
System.ServiceModel.Security.MessageSecurityException:安全处理器无法在消息中找到安全标头。这可能是因为消息是不安全的错误,或者是因为通信方之间存在绑定不匹配。如果服务配置为安全性,而客户端未使用安全性,则可能发生这种情况。


我的问题是我需要什么额外的安全头,以及如何修改绑定以包含它们?

消息说服务配置为安全性,而客户端未使用安全性。
要使客户端配置自动生成,您可以使用“添加服务引用”选项,或使用SvcUtil.exe生成代理类。

如果您想了解Microsoft如何看待本文并下载源代码。它是为人们发布的,用于构建Windows 8 Phone和RT应用程序(不支持.NET 4和WIF),但也可用于.NET 3.5项目(不能使用.NET 4 DLL)