使用Windows凭据在CRM 2011中实例化OrganizationServiceProxy

问题描述:

是否有人试图使用Windows凭据在CRM 2011(内部部署)中创建OrganizationServiceProxy实例?我有一个使用使用Windows凭据在CRM 2011中实例化OrganizationServiceProxy

 <security mode="TransportCredentialOnly"> 
     <transport clientCredentialType="Windows"/> 
     </security> 

一个WCF服务,我可以确认用户的身份验证(OperationContext.Current.ServiceSecurityContext.WindowsIdentity.IsAuthenticated),但我不知道如何生成/传递ClientCredentials到创建一个CRM服务的实例。这是从Silverlight应用程序中调用的,该应用程序不在CRM内的IFrame中。

谢谢。

需要使用单独的用户帐户登录到OrganizationServiceProxy。 您无法检索Windows凭据以传递给代理进行身份验证。

您使用的用户需要与其关联的prvActOnBehalfOfAnotherUser权限。

完成此操作后,您可以成功登录并检索有效的OrganizationServiceProxy,您需要作为服务的使用者执行的操作是在您调用操作时指定CallerId。您应该使用Xrm.Page.context.getUserId从xrm模型中检索此令牌。看到。 http://msdn.microsoft.com/en-us/library/gg334511.aspx

然后从silverlight中,您将使用System.Windows.Browser.ScriptObject网桥执行客户端JavaScript以检索登录到crm的当前用户的用户标识。 最好在应用程序引导时执行此操作,并将值保存到applicationdata变量中,以便从Silverlight应用程序中全局访问它。

例如,的客户端脚本。

function CrmContext() { 
} 

var context = null; 
with (window.parent) { 
    context = Xrm.Page.context;} 

CrmContext.prototype.ReadUserId = function() { 
    var userId = context.getUserId(); 
    return userId; 
} 

一旦你的用户令牌设置代理服务器与来电显示,该值

EG。

private OrganizationServiceProxy Proxy { get; set; } 

public Guid Create(CreateEntity request) 
{ 
    if (request == null || request.UserId == Guid.Empty || request.Entity == null) 
    { 
     throw new InvalidMessageException("Invalid reqest message. Please provide compulsory criteria"); 
    } 

    var result = Guid.Empty; 

    try 
    { 
     if (Proxy != null) 
     { 
      Proxy.CallerId = request.UserId; 

      using (Proxy) 
      { 
       result = Proxy.Create(request.Entity); 
      } 
     } 
    } 
    catch (FaultException<OrganizationServiceFault> e) 
    { 
     Log.Error(e.Message); 
     throw new IntegrationException(e.Message); 
    } 

    return result; 
} 

解决这个所采取的方法IVE是创建一个CRM适配器包封CRM代理和发送请求的对象,其包括用户令牌的服务接口。

public OrganizationServiceAdapter(ICrmConfigurationContext crmConfigurationConext) 
{ 
    try 
    { 
     Proxy = new OrganizationServiceProxy(
      crmConfigurationConext.OrganizationServiceConfiguration, 
      crmConfigurationConext.Credentials); 
    } 
    catch (Exception e) 
    { 
     //// TODO: Add local proxy pattern implementation for failover 
     Proxy = null; 
     Log.Error(e.Message); 
     throw new IntegrationException(ExceptionMessages.CouldNotLoginToOrganizationService()); 
    } 
}