我正在开发一个C#应用程序,用于从Dynamics CRM Online检索数据。要验证Dynamics CRM的用户名和密码,我使用的是WhoAmIRequest。它工作正常,直到出现以下情况。
1)使用有效URL,用户名和密码连接Dynamics CRM。
2)处置组织服务对象。
3)使用有效URL,用户名和无效密码重新连接Dynamics CRM。
在这种情况下,WhoAmIRequest也成功执行了。但它应该失败。
以下是我使用的代码:
private void button6_Click(object sender, EventArgs e)
{
CrmConnection connection;
string url = "Url=https://mytest.crm.dynamics.com ;[email protected]; Password=goodpassword;";
connection = CrmConnection.Parse(url);
OrganizationService orgService = new OrganizationService(connection);
Guid userid = ((WhoAmIResponse)orgService.Execute(new WhoAmIRequest())).UserId;
if (userid == null)
MessageBox.Show("Login Failed");
else
MessageBox.Show("Login Success");
orgService.Dispose();
url = "Url=https://mytest.crm.dynamics.com ;[email protected]; Password=badpassword;";
connection = CrmConnection.Parse(url);
orgService = new OrganizationService(connection);
userid = ((WhoAmIResponse)orgService.Execute(new WhoAmIRequest())).UserId;
if (userid == null)
MessageBox.Show("Login Failed");
else
MessageBox.Show("Login Success");
orgService.Dispose();
url = "Url=https://mytest.crm.dynamics.com ;[email protected]; Password=goodpassowrd;";
connection = CrmConnection.Parse(url);
orgService = new OrganizationService(connection);
userid = ((WhoAmIResponse)orgService.Execute(new WhoAmIRequest())).UserId;
if (userid == null)
MessageBox.Show("Login Failed");
else
MessageBox.Show("Login Success");
orgService.Dispose();
}
上面代码的输出显示3个消息框为
登录成功
登录成功
登录成功
但它应该显示为
登录成功
登录失败
登录成功
我也尝试过Nicknow在Need to validate CRM credentials问题中提出的答案,但没有任何帮助
任何帮助将受到高度赞赏。
谢谢并问候Venkatesan
问题在于您的支票:
if (userid == null)
UserId是一个Guid,Guid是一个struct,一个struct是一个值类型,一个值类型永远不会是null
,所以check总是返回false。
有关更多信息,请参阅此处Guid == null should not be allowed by the compiler
我建议使用以下检查:
if (userid == Guid.Empty)
您需要更改此行,因为这是一个静态方法:
connection = CrmConnection.Parse(url);
对于这样的事情:
connection = new CrmConnection();
connection.ServiceUri = new Uri("https://mytest.crm.dynamics.com");
var clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "[email protected]";
clientCredentials.UserName.Password = "goodpassword";
connection.ClientCredentials = clientCredentials;
我遇到了同样的问题,我找到了解决方案!所以这是我的连接字符串:
URL = mytestcrm.crm.dynamics.com; [email protected];密码= myPassword;复制代码的authType = Office365; RequireNewInstance =真
注意连接字符串末尾的“RequireNewInstance = True”,它会做到这一点。
试一试,它将解决问题。