我正在使用 Microsoft CRM 4.0 SDK 连接到面向 Internet 的部署 (IFD)。我可以调用发现服务并取回组织列表。当我尝试使用其中一个组织时,我收到 401 Unauthorized 错误。我试图将其归结为最简单的程序来证明这一点。代码(更改了 URL、用户名和密码)如下所示。我希望这能输出组织中的帐户数量。当我运行它时,我在使用该服务时收到 401 Unauthorized 异常。
// Get orgs from disco service
CrmDiscoveryService disco = new CrmDiscoveryService();
disco.Url = "https://notthereal.crmurl.com/MSCRMServices/2007/SPLA/CrmDiscoveryService.asmx";
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
orgRequest.UserId = @"mydomain\myuser";
orgRequest.Password = "mypassword";
RetrieveOrganizationsResponse orgResponse =
(RetrieveOrganizationsResponse)disco.Execute(orgRequest);
// Find the test org
OrganizationDetail orgInfo = null;
foreach (OrganizationDetail orgdetail in orgResponse.OrganizationDetails)
{
if (orgdetail.OrganizationName.Equals("TestOrganization"))
{
orgInfo = orgdetail;
break;
}
}
if (orgInfo == null)
{
throw new Exception("The specified organization was not found.");
}
// Get a CRM ticket
RetrieveCrmTicketRequest ticketRequest = new RetrieveCrmTicketRequest();
ticketRequest.OrganizationName = orgInfo.OrganizationName;
ticketRequest.UserId = @"mydomain\myuser";
ticketRequest.Password = "mypassword";
RetrieveCrmTicketResponse ticketResponse
= (RetrieveCrmTicketResponse)disco.Execute(ticketRequest);
// Create an authorization token
CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
sdktoken.OrganizationName = orgInfo.OrganizationName;
sdktoken.CrmTicket = ticketResponse.CrmTicket;
CrmService service = new CrmService();
service.CrmAuthenticationTokenValue = sdktoken;
service.Url = orgInfo.CrmServiceUrl;
// Retrieve the accounts
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = new QueryExpression("account");
// 401 EXCEPTION WHEN EXECUTING NEXT LINE
var response = (RetrieveMultipleResponse)service.Execute(request);
// Show the count of accounts
Console.WriteLine(response.BusinessEntityCollection.BusinessEntities.Count);
迪斯科服务请求正常工作,但 CRM 服务请求失败,这对我来说似乎很奇怪。