CRM 2011 Online通过ASP.net应用程序无法运行,相同的代码通过Console Application Works - > “身份验证失败” - 错误

时间:2013-07-25 11:02:33

标签: asp.net dynamics-crm-2011 dynamics-crm-online svc

我正在尝试连接到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”-solution ...

上却没有

PS:我能够通过crmsvcutil.exe / url生成“context”文件:https://org.crm4.dynamics.com/XRMServices/2011/Organization.svc /o:crm.cs / u:username / p:password / di:deviceUserName / dp:devicPWD

1 个答案:

答案 0 :(得分: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和密码,以便您可以查看它们是否已设置。