Dynamics CRM SDK in C # connects using Invalid Password

I am developing a single C # application that is used to extract data from Dynamics CRM Online. To verify the username and password in CRM Dynamics, I use WhoAmIRequest. It works fine until the script below appears.

1) Connect Dynamics CRM with a valid URL, username and password.

2) Delete the organization’s service object.

3) Reconnect Dynamics CRM with a valid URL, username, and invalid password.

In this case, WhoAmIRequest also successfully completed. But he must fail.

The following is the code I'm using:

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(); } 

The code output above shows 3 message boxes as

Login Success

Login Success

Login Success

But it should display as

Login Success

Login failed

Login Success

I also tried to suggest Nicknow's answer in Need to verify CRM credentials Question, but nothing helps

Any help would be greatly appreciated.

Thanks and Regards Venkatesan

+6
source share
1 answer

The problem is in your check here:

if (userid == null)

UserId is Guid, Guid is struct, struct is the value type, and the value type will never be null , so the check always returns false.

See here for more info. Guid == null should not be resolved by the compiler

Instead, I would suggest using the following check:

if (userid == Guid.Empty)

+4
source

Source: https://habr.com/ru/post/989656/


All Articles