0

我正在使用 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 服务请求失败,这对我来说似乎很奇怪。

4

1 回答 1

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.dllMSDN中所述

于 2015-04-02T07:45:06.323 回答