您需要使用单独的用户帐户登录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 的用户ID。
最好在应用程序引导时执行此操作,并将值保存到 applicationdata 变量中,以便可以从您的 silverlight 应用程序中全局访问它。
例如。客户端脚本。
function CrmContext() {
}
var context = null;
with (window.parent) {
context = Xrm.Page.context;}
CrmContext.prototype.ReadUserId = function () {
var userId = context.getUserId();
return userId;
}
一旦你有了用户令牌,用这个值设置 Proxy CallerId
例如。
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;
}
我解决这个问题的方法是创建一个封装 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());
}
}