c# - 是否可以在没有 SDK 的情况下调用 Dynamics CRM 2011 后期绑定(bind) WCF 组织服务 - 直接自定义绑定(bind)?

标签 c# wcf dynamics-crm-2011 wcf-security wcf-client

我正在尝试实现一个纯 WCF 场景,我想在不依赖 SDK 帮助程序类的情况下调用 Dynamics CRM WCF 服务。基本上,我想仅使用 .net 框架的 native WCF 支持来针对 Dynamics CRM 2011 实现联合身份验证。

我这样做的原因是我想稍后将这个场景移植到 BizTalk。

我已经使用 SvcUtil 成功生成了代理类,但是部分策略和安全断言与配置架构不兼容。 SvcUtil 建议改为从代码构建绑定(bind),这正是我正在尝试做的。

结果代码在这里:

        private static void CallWcf()
    {
        OrganizationServiceClient client = null;

        try
        {
            // Login Live.com Issuer Binding

            var wsHttpBinding = new WSHttpBinding();
            wsHttpBinding.Security = new WSHttpSecurity();
            wsHttpBinding.Security.Mode = SecurityMode.Transport;

            // Endpoint Binding Elements

            var securityElement = new TransportSecurityBindingElement();
            securityElement.DefaultAlgorithmSuite = SecurityAlgorithmSuite.TripleDes;
            securityElement.IncludeTimestamp = true;
            securityElement.KeyEntropyMode = SecurityKeyEntropyMode.CombinedEntropy;
            securityElement.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrust13WSSecureConversation13WSSecurityPolicy12BasicSecurityProfile10;
            securityElement.SecurityHeaderLayout = SecurityHeaderLayout.Strict;

            var securityTokenParameters = new IssuedSecurityTokenParameters();
            securityTokenParameters.InclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient;
            securityTokenParameters.ReferenceStyle = SecurityTokenReferenceStyle.Internal;
            securityTokenParameters.RequireDerivedKeys = false;
            securityTokenParameters.TokenType = null;
            securityTokenParameters.KeyType = SecurityKeyType.SymmetricKey;
            securityTokenParameters.KeySize = 192;
            securityTokenParameters.IssuerAddress = new EndpointAddress("https://login.live.com/extSTS.srf");
            securityTokenParameters.IssuerMetadataAddress = null;
            securityTokenParameters.DefaultMessageSecurityVersion = null;
            securityTokenParameters.IssuerBinding = wsHttpBinding;

            securityElement.EndpointSupportingTokenParameters.Signed.Add(securityTokenParameters);

            var textMessageEncodingElement = new TextMessageEncodingBindingElement();
            textMessageEncodingElement.MaxReadPoolSize = 64;
            textMessageEncodingElement.MaxWritePoolSize = 16;
            textMessageEncodingElement.MessageVersion = MessageVersion.Default;
            textMessageEncodingElement.WriteEncoding = System.Text.Encoding.UTF8;

            textMessageEncodingElement.ReaderQuotas.MaxStringContentLength = 8192;
            textMessageEncodingElement.ReaderQuotas.MaxArrayLength = 16384;
            textMessageEncodingElement.ReaderQuotas.MaxBytesPerRead = 4096;
            textMessageEncodingElement.ReaderQuotas.MaxNameTableCharCount = 16384;

            var httpsTransportElement = new HttpsTransportBindingElement();
            httpsTransportElement.ManualAddressing = false;
            httpsTransportElement.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;

            CustomBinding binding = new CustomBinding();
            binding.Elements.Add(securityElement);
            binding.Elements.Add(textMessageEncodingElement);
            binding.Elements.Add(httpsTransportElement);

            client = new OrganizationServiceClient(binding, new EndpointAddress(EndpointUri));
            client.ClientCredentials.UserName.UserName = Username;
            client.ClientCredentials.UserName.Password = Password;
            client.Open();

            var columnSet = new schemas.microsoft.com.xrm._2011.Contracts.ColumnSet();
            var identifier = new Guid("fbf8240e-2c85-e011-ad55-1cc1de0878eb");

            columnSet.Columns = new string[] { "name" };
            var entity = client.Retrieve("account", identifier, columnSet);
        }

        finally
        {
            if (client != null)
                client.Close();
        }
    }

我是联合身份验证的新手,很难弄清楚许多可用绑定(bind)之间的潜在差异,因此我将不胜感激在这方面的任何帮助。

最佳答案

这可能是可能的,但非常复杂。我们有一个使用 Dynamics 的项目,该项目已迁移到 ADFS,并且需要围绕刷新 token 添加大量额外代码(来自 SDK 的代码形式 autorefreshsecuritytoken.cs、deviceidmanager.cs 和 toolserviceproxies.cs),并且该项目仍在使用 SDK 处理所有内容。

请记住,您还需要在操作系统中安装 windows.identification,这是要复制的另一项功能负载。

最后,您始终可以只使用 JustDecompile 或类似软件来查看 SDK 在做什么。

关于c# - 是否可以在没有 SDK 的情况下调用 Dynamics CRM 2011 后期绑定(bind) WCF 组织服务 - 直接自定义绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6775856/

相关文章:

c# - 如何自动将 const wchar_t* 从 C DLL 转换为 C# 字符串

c# - 访问共享网络驱动器时需要模拟

wcf - WCF 的 SSL 不工作!如何调试

wcf - WCF服务中基地址的用途是什么

wcf - WCF 的 RESTful 框架替代品

view - 如何加快 Microsoft Dynamics CRM 2011 中的查询速度

c# - 触发事件时出现 NullReferenceException

c# - MySQL 解释生成的查询中的特殊字符

.net - 作为共享过程的一部分,MS CRM 2011 在哪个实体中创建记录?

dynamics-crm - 如何在事件 View 中添加 Dynamics CRM 自定义字段?