Need to validate CRM credentials
Asked Answered
A

1

2

My goal is simple. Using the Dynamics CRM 2013 API and given a url, user name, password, and domain, how can I check that the values are valid?

I have a custom application that will be using the API. I have a setup screen where the user will enter the URL, user name, password, and domain. I also have a button called “Test Connection” that will verify that it can connect using that information.

Here is the logic I am using to create the connection:

string connectionString;
if (_crmDomain != "")
    connectionString = string.Format(@"Url={0}; Username={1}\{2}; Password={3}", url, domain, userName, password);
else
    connectionString = string.Format(@"Url={0}; Username={1}; Password={2}", url, userName, password);

var css = new ConnectionStringSettings("CRMConnectionString", connectionString);
var cn = new CrmConnection(css);
var service = new OrganizationService(cn);

The issue is that even if the credentials are invalid (perhaps an incorrect password), the line that creates the new OrganizationService works fine. Is there any way to check that it will work when an actual call is eventually made?

Right now I am getting around it by making a dummy call. This forces it to create the connection. Here is what I’m doing:

var request = new RetrieveAllEntitiesRequest();
request.EntityFilters = EntityFilters.Entity;
service.Execute(request);

If the credentials are not valid then the service.Execute line throws an exception. This works fine with one problem. Let’s say the user entered valid credentials and successfully clicked Test Connection. Now let’s say they change the password to be wrong and click Test Connection. In this case it is still succeeding. It seems to use the previously existing connection. Is there a way to wipe out or clear the previous connection so that when I try with invalid values it will throw an exception?

Again, my overall goal is to validate the login values. Whatever will provide the best way of accomplishing this is what I’m after. Thanks!

Ambi answered 8/10, 2014 at 22:2 Comment(1)
msdn.microsoft.com/en-us/library/…Proportioned
C
3

Drop the use of ConnectionStringSettings and use CrmConnection.Parse().

var cn = CrmConnection.Parse(connectionString);
var service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
}
catch (Exception ex)
{
    //Do something because connection setup was bad
}

UPDATED

I ran the following code and got the expected output, shown below the code. When the password is changed to a bad password the request fails and when changed to a good password it succeeds.

var connectionString = @"Url=https://server.com/orgname/;Username=domain\username;Password=goodpassword;";
var cn = CrmConnection.Parse(connectionString);
var service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
    Console.WriteLine("Good Password");
}
catch
{
    Console.WriteLine("Bad Password");    
}

connectionString = @"Url=https://server.com/orgname/;Username=domain\username;Password=badpassword;";
cn = CrmConnection.Parse(connectionString);
service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
    Console.WriteLine("Good Password");
}
catch
{
    Console.WriteLine("Bad Password");    
}

connectionString = @"Url=https://server.com/orgname/;Username=domain\username;Password=goodpassword;";
cn = CrmConnection.Parse(connectionString);
service = new OrganizationService(cn);

try
{           
    service.Execute(new WhoAmIRequest());
    Console.WriteLine("Good Password");
}
catch
{
    Console.WriteLine("Bad Password");    
}

The output to console is:

Good Password
Bad Password
Good Password
Cither answered 9/10, 2014 at 4:46 Comment(2)
Thanks. It's interesting in that using this method, after I successfully open a connection, if I change the URL or User Name to something invalid then it fails when I try again. However, if I change the password to something invalid, it is still successful. So that works better but not quite.Ambi
Please post additional code to demonstrate the entire process so we can try to reproduce. I updated my answer with code that I was able to get to successfully execute.Cither

© 2022 - 2024 — McMap. All rights reserved.