When connecting to an online version, you have to give device credentials as well as user credentials. Just the way it is (Windows live authentication scheme and all.)
Edit
After some bit of research, I still have not found official documentation as to why this is. Generally, on the MSDN forums it is accepted that this is to associate give a device identity to windows live. Maybe this is so Microsoft can track what workstations are using CRM? Maybe it enables them to enhance security at some point by restricting which machine identities may connect to CRM using certain accounts. Maybe all/none of the above.
Speculation aside, in my experience, I have not been able to execute a successful query without device credentials authenticated against the CRM (i.e. through the OrganizationServiceProxy
constructor or the Authentication process used by the IServiceManagment implementations.)
Below, you will find some older code that I was using to do this. I now recommend using the connection string as mentioned by @Jason Lattimer. If you need to handle more parallelism, look into using IServiceManagement<IOrganizationService>
.
var reader = new AppSettingsReader();
//instatantiate credential class and populate values
var cc = new ClientCredentials();
cc.UserName.UserName = reader.GetValue("WLID", typeof(string)).ToString();
cc.UserName.Password = reader.GetValue("WLPS", typeof(string)).ToString();
//repeat for device credentials
var deviceCredentials = new ClientCredentials();
deviceCredentials.UserName.UserName = reader.GetValue("deviceWLID", typeof(string)).ToString();
deviceCredentials.UserName.Password = reader.GetValue("deviceWLPS", typeof(string)).ToString();
//create a uri for the organization service location
#if DEBUG
var orgServiceUri = new Uri(reader.GetValue("CrmNonProductionUri", typeof(string)).ToString());
#else
Uri orgServiceUri = new Uri(reader.GetValue("CrmProductionUri", typeof(string)).ToString());
#endif
OrganizationServiceProxy retval = new OrganizationServiceProxy(orgServiceUri, null, cc, deviceCredentials);
retval.EnableProxyTypes();
So, in the end, I just use a config file to store the various values needed to construct my credentials and uri (for both environments.)