0

我正在尝试连接到 CRM 2011 Online 环境。我可以通过“控制台应用程序”进行连接,但是当我尝试通过具有相同代码的“ASP.net”应用程序进行连接时,它不起作用,它给了我“身份验证失败”-错误({“从另一方收到不安全或不正确安全的故障。有关故障代码和详细信息,请参见内部的 FaultException。”})。

我们需要做一些特别的事情来使它在“ASP.net”环境中工作。我测试了我在互联网上找到的几种解决方案,但都给了我同样的错误。

我的简化代码的“代码”片段:

    private static ClientCredentials GetDeviceCredentials()
    {
        return Microsoft.Crm.Services.Utility.DeviceIdManager.LoadOrRegisterDevice();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Authenticate using credentials of the logged in user;
        string UserName = "*****";   //your Windows Live ID
        string Password = "*****";    // your password
        ClientCredentials Credentials = new ClientCredentials();
        Credentials.UserName.UserName = UserName;
        Credentials.UserName.Password = Password;

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        //This URL needs to be updated to match the servername and Organization for the environment.
        Uri OrganizationUri = new Uri("https://*****.crm4.dynamics.com/XRMServices/2011/Organization.svc");           //this URL could copy from Setting --> Developer Source 

       Uri HomeRealmUri = null;
        //OrganizationServiceProxy serviceProxy;   
       using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, GetDeviceCredentials()))
       {
           IOrganizationService service = (IOrganizationService)serviceProxy;
           OrganizationServiceContext orgContext = new OrganizationServiceContext(service);

           var theAccounts = orgContext.CreateQuery<Account>().Take(1).ToList();
           Response.Write(theAccounts.First().Name);
       }

    }

我尝试了几件事,例如删除“LiveDeviceID”文件夹的内容并重新运行设备注册工具。但奇怪的是它在“控制台应用程序”中有效,但在我的“asp.net”解决方案中无效......

PS:我可以通过 crmsvcutil.exe /url 生成“上下文”文件:https: //org.crm4.dynamics.com/XRMServices/2011/Organization.svc /o:crm.cs /u:username / p:密码 /di:deviceUserName /dp:devicPWD

4

1 回答 1

0

你有什么特别的原因吗

Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

您不需要该行进行 Windows Live 身份验证。

即使这样,代码似乎也是有效的,因此它与设备注册有关。我建议而不是像你一样直接调用它

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, GetDeviceCredentials()))
   {

您尝试以下操作,因为您只需要注册一次:

ClientCredentials deviceCredentials;

if ((CRMSettings.Default.DeviceID == String.Empty) || (CRMSettings.Default.DevicePassword == String.Empty))
{
    deviceCredentials = Microsoft.Crm.Services.Utility.DeviceIdManager.RegisterDevice();
}
else
{
    deviceCredentials = new ClientCredentials();
    deviceCredentials.UserName.UserName = CRMSettings.Default.DeviceID;
    deviceCredentials.UserName.Password = CRMSettings.Default.DevicePassword;
}

using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, deviceCredentials))
{

过去我遇到过问题,我从 RegisterDevice 调用中得到“已经注册”的响应。

我还将转储设备 ID 和密码,以便您查看是否正在设置它们。

于 2013-07-25T22:54:00.900 回答