我正在使用Microsoft CRM 4.0 SDK连接到Internet Facing Deployment(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服务请求爆炸似乎很奇怪。
答案 0 :(得分:0)
当我遇到同样的问题时,为其他人发布答案......
看起来Richard已为AuthenticationType
添加了错误的值CrmAuthenticationToken sdktoken = new CrmAuthenticationToken();
sdktoken.AuthenticationType = 2;
根据MSDN
The AuthenticationType class exposes the following members.
Field Value Description
AD 0 Specifies Active Directory authentication.
Passport 1 Specifies Windows Live ID authentication.
Spla 2 Specifies Internet-Facing Deployment authentication (formerly known as SPLA).
AuthenticationType
枚举可以通过添加Microsoft.Crm.sdk.dll
的引用来使用here in MSDN