C#中的Dynamics CRM SDK使用无效密码连接

我正在开发一个C#应用程序,用于从Dynamics CRM Online检索数据。 要validationDynamics 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 ;Username=mytest@mytest.onmicrosoft.com; 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 ;Username=mytest@mytest.onmicrosoft.com; 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 ;Username=mytest@mytest.onmicrosoft.com; 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在需要validationCRM凭证问题时提出的答案,但没有任何帮助

任何帮助将受到高度赞赏。

谢谢并问候Venkatesan

问题在于您的支票:

if (userid == null)

UserId是Guid,Guid是结构,struct是值类型,值类型永远不会为null ,因此check始终返回false。

有关更多信息,请参见此处编译器不应允许Guid == null

我建议使用以下检查:

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 = "mytest@mytest.onmicrosoft.com"; clientCredentials.UserName.Password = "goodpassword"; connection.ClientCredentials = clientCredentials;