Dynamics CRM SDK in C# connect with Invalid Password
Asked Answered
Q

3

7

I am developing one C# application which is used to retrieve data from Dynamics CRM Online. To validate the User Name and Password of Dynamics CRM I am using the WhoAmIRequest. It works fine until the below scenario occures.

1)Connect the Dynamics CRM with Valid URL, User Name and Password.

2) Dispose the Organization Service Object.

3) Reconnect the Dynamics CRM with Valid URL, User Name and Invalid Password.

In this scenario also the WhoAmIRequest got executed Successfully. But it should fail.

Below is the code i am using:

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

The output of above code shows 3 message box as

Login Success

Login Success

Login Success

But it should show as

Login Success

Login Failed

Login Success

I have also tried the answer suggest by Nicknow in the the Need to validate CRM credentials Question but nothing helps

Any help will be highly appreciated.

Thanks and Regards Venkatesan

Quarterphase answered 24/6, 2015 at 6:56 Comment(4)
When I try to do this, it ask me for credentials, do you get a valid user in that response?Thorin
Thanks for the response Sxntk, It is not the valid credential i have just modified to some invalid for example purpose hence the credential which i am using is restricted to share. You can create a trial account in dynamics crm for testing purposes. Let me know if you want the credential.Quarterphase
I'm trying this on my crm on premise, so when I dispose the service and try to authenticate with a wrong password active directory pops up, so if I don't provide the correct credentials it fails.Thorin
Thanks for the response Sxntk, May be you are correct, I have only used it for connecting with Online versions. I didn't tested it with On premise.Quarterphase
W
4

The problem is in your check here:

if (userid == null)

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

See here for more information Guid == null should not be allowed by the compiler

I would suggest using the following check instead:

if (userid == Guid.Empty)

Women answered 24/6, 2015 at 7:44 Comment(1)
Thanks for the Suggestion James, I have tried your suggestion and got the same result. It accepts the invalid password only in the following scenario. 1)Connect the Dynamics CRM with Valid URL, User Name and Password. 2) Dispose the Organization Service Object. 3) Reconnect the Dynamics CRM with Valid URL, User Name and Invalid Password. When we try to login with invalid password at the very fist attempt it shows the authentication failure message corretly.Quarterphase
S
0

You need to change this line because that is a static method:

connection = CrmConnection.Parse(url);

To something like:

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;
Sam answered 26/4, 2018 at 9:19 Comment(0)
E
0

I was experiencing the same issue and i found a solution for this! So here is my connection string:

Url=mytestcrm.crm.dynamics.com;[email protected];Password=MyPassword;authtype=Office365;RequireNewInstance=True

Note the "RequireNewInstance=True" to the end of the connection string, it will do the trick.

Give it a try and it will resolve the issue.

Exequies answered 25/3, 2019 at 14:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.