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!