Hans Rasmussen

info@hansrasmussen.com, +46 (0)723 207008

CRM Authentication

One good way of Authenticating against CRM Webservice is not tell CRM which internally defined system user account you wish to authenticate against. I found this good article from Microsoft about how to lookup the system user account based on the GUID that it receives in CRM after you have told CRM that you wish to create a user for this account.

http://msdn.microsoft.com/en-us/library/cc151052.aspx

//extract from the link above

using System;
 

using System.Web.Services.Protocols;
 

using System.Text;
 

using System.Net;
 

using System.Xml;
namespace Microsoft.Crm.Sdk.Reference

{
// Microsoft Dynamics CRM namespaces
using CrmSdk;

using
 

CrmSdk.Discovery;
public class Impersonation
 
 

 

{
[STAThread]public static void Main(string[] args)

{
CrmAuthenticationToken token = new CrmAuthenticationToken

();
token.AuthenticationType = 0; // Use Active Directory authentication.
token.OrganizationName = “AdventureWorksCycle”;
 

 

// Use the global user ID of the system user that is to be impersonated.
token.CallerId = new Guid(“94092D6F-B367-DC11-9C93-0003FFDFCE28″);

CrmService crmService = new CrmService();crmService.Url =
“http://localhost/MSCRMServices/2007/CrmService.asmx”;

crmService.CrmAuthenticationTokenValue = token;
crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Create a new account owned by the impersonated user.
account account = new account();

account.name = “Fabrikam”;
Guid accountid = crmService.Create(account);

}
}
}

// my own code comes here

The other way is of course to authenticate youself by providing this information to CRM Web service.

CrmService service = null;
 

service = new CrmService();
 

service.Url = “http://crm/MSCrmServices/2007/CrmService.asmx”;
 

CrmAuthenticationToken token = new CrmAuthenticationToken();
 

token.AuthenticationType = 0;
token.OrganizationName = “putyourorganizationnamehere”;

System.Net.ICredentials myCredentials;myCredentials =
new NetworkCredential(“useraccount”, “password****”, “domain”);

service.Credentials = myCredentials;
service.PreAuthenticate = true;

service.CrmAuthenticationTokenValue = token;
// do your stuff on service object (same as the above example)
// Create a new account owned by the impersonated user.

account account = new account();

account.name = “Fabrikam”;

Guid accountid = crmService.Create(account);

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

*