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