【发布时间】:2013-09-10 21:47:51
【问题描述】:
我在 Web 服务器 A 上有一个 MVC4 Web 应用程序,它使用 Web 服务器 B 上的 OrganizationServiceProxy 使用 Dynamics CRM Web 服务。MVC4 应用程序设置为启用了 ASP .NET 模拟和 Windows 身份验证。当我打电话给 WhoAmI 时,我收到一个错误:
'调用者未经服务验证。'
现在,如果我将 MVC4 应用程序移动到具有与 Web 服务器 A 上相同的身份验证的 Web 服务器 B(与 CRM 相同),它会毫无例外地调用 WhoAmI。
这是用于连接服务器的代码。
string serviceURL = ConfigurationManager.AppSettings["CRMROOTURL"].ToString() + "XRMServices/2011/Organization.svc";
this.CRMService = GetCRMService(serviceURL);
private OrganizationServiceProxy GetCRMService(string serviceURL)
{
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
OrganizationServiceProxy client
= new OrganizationServiceProxy(new Uri(serviceURL), null, credentials, null);
return client;
}
这是 IIS 网站上的身份验证屏幕截图。
根据正确答案,我只是想提供一些 sn-ps 来帮助其他人。
string loggedUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new NetworkCredential(username, password, domain);
OrganizationServiceProxy client
= new OrganizationServiceProxy(new Uri(serviceURL), null, credentials, null);
client.ClientCredentials.Windows.ClientCredential = credentials.Windows.ClientCredential;
// -- Retrieve the user.
QueryExpression expression = new QueryExpression
{
EntityName = "systemuser",
ColumnSet = new ColumnSet("systemuserid")
};
expression.Criteria.AddCondition("domainname", ConditionOperator.Equal, loggedUser);
EntityCollection ec = client.RetrieveMultiple(expression);
if (ec.Entities.Count > 0)
{
// -- Impersonate the logged in user.
client.CallerId = ec.Entities[0].Id;
}
谢谢!
【问题讨论】:
-
托管 MVC4 Web 应用程序的应用程序池的应用程序标识在两台服务器之间是否不同?
-
它们目前彼此不同。我应该使它们相同并尝试吗?
-
GetCRMService真的没有帮助。我需要查看调用 OrganizationServiceProxy 的构造函数的代码,更重要的是,您要传递给它的凭据是什么,以及凭据是如何被实例化的。 -
对不起,我在帖子中添加了这个功能。
-
如果在设置 CallerId 之前使用它,请不要使用相同的 OrganizationServiceProxy 对象,这一点非常重要。 callerid 被缓存,您的调用将失败。获得要模拟的用户的 guid 后,您需要创建一个新的 OrganizationServiceProxy 对象并设置该对象的 CallerId。
标签: wcf dynamics-crm-2011