Monday, August 18, 2008

Microsoft Dynamics CRM IFD Process

Microsoft CRM uses active directory to authenticate users. CRM 4.0 supports Internet facing domains and Multilingual applications. The user needs to be registered within the Active directory to access the CRM Application. In my last project I had a chance to explore CRM to the maximum level. In addition to the out of the box functionalities, we had to create some bespoke web applications which are integrated within CRM.

The bespoke application developed in c# using asp.net needs to support the context of the users logged into CRM. So, when we create a CRM Service using the asp.net application, we need to pass in the Context of the asp.net page to the CRM Service class and impersonate the user for creating the service token. The sample code for creating the service is as follows:


private CrmService GetCrmService()
{
try
{
//Services
CrmService crmService = new CrmService();
crmService.CrmAuthenticationTokenValue = GetToken();
crmService.Credentials = CredentialCache.DefaultCredentials;
crmService.Url = CrmServiceUrl;

return crmService;
}
catch (SoapException ex)
{
throw new ExceptionHandler(ex);
}
catch (Exception ex)
{
throw new ExceptionHandler(ex);
}
}

The CRM Token creation needs to be impersonated for the Context user and the CrmAuthentication to work.


private CrmSDK.CrmAuthenticationToken GetToken()
{
try
{

using (new CrmImpersonator())
{
CrmSDK.CrmAuthenticationToken crmToken = new
CrmSDK.CrmAuthenticationToken();

Microsoft.Crm.Sdk.CrmAuthenticationToken token;
if (Context.User.Identity.AuthenticationType
== "CrmPostAuthentication")
{
token =
Microsoft.Crm.Sdk.CrmAuthenticationToken.ExtractCrmAuthenticationToken
(Context, OrganizationName);
}
else
{
token = new Microsoft.Crm.Sdk.CrmAuthenticationToken();
token.OrganizationName = OrganizationName;
}
token.AuthenticationType = authenticationType;
token.OrganizationName = OrganizationName;

crmToken.Actor = token.Actor;
crmToken.AuthenticationType = token.AuthenticationType;
crmToken.CallerId = token.CallerId;
crmToken.CrmTicket = token.CrmTicket;
crmToken.DidUnderstand = token.DidUnderstand;
crmToken.EncodedMustUnderstand = token.EncodedMustUnderstand;
crmToken.EncodedMustUnderstand12 = token.EncodedMustUnderstand12;
crmToken.EncodedRelay = token.EncodedRelay;
crmToken.MustUnderstand = token.MustUnderstand;
crmToken.OrganizationName = token.OrganizationName;
crmToken.Relay = token.Relay;
crmToken.Role = token.Role;
return crmToken;
}
}
catch (SoapException ex)
{
throw new ExceptionHandler(ex);
}
catch (Exception ex)
{
throw new ExceptionHandler(ex);
}

}

Create a Microsoft.Crm.Sdk.CrmAuthenticationToken token for the specific context and assign the token properties to the CrmSDK.CrmAuthenticationToken. All these need to be done within the CrmImpersonator for the asp.net Context to work.

No comments: