Thursday, November 18, 2010

Impersonation in custom aspx page for logged in user

To execute crmservice calls from the logged in user context




CrmService crmservice = new CrmService();
            string orgName = "contoso";

            using (new CrmImpersonator())
            {
                CrmAuthenticationToken token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgName);
                token.AuthenticationType = 0;
                crmservice.Url = "http://crm//MSCRMServices//2007//crmservice.asmx";
                crmservice.CrmAuthenticationTokenValue = token;
                crmservice.Credentials = CredentialCache.DefaultCredentials;




                try
                {
                    //Any crmservice call 
                   //crmservice.Execute
                }
                catch (Exception ex)
                {
                }


            }


Remember that the calls have to be executed within the using block.
Else, it results in an exception. 

No comments:

Post a Comment