Export (0) Print
Expand All

CrmImpersonator Class (Sdk Assembly)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Allows a block of code to execute under the process credentials instead of the running thread's identity.

The CrmImpersonator class implements the IDisposable interface. It exposes the following members.

CrmImpersonatorInitializes a new instance of the CrmImpersonator class.

Instance MethodDescription
DisposeReleases the resources that are used by the CrmImpersonator instance.


This must be used inside a using statement. At the end of the using statement, execution will return to running under thread id.

Note    When you are using this class, you must set the ownerid property for the entity when you use the Create method or Create message.

For more information about impersonation, see the MSDN documentation, located at http://msdn2.microsoft.com/en-us/library/aa376391.aspx.


This shows how to use the CrmImpersonator class. For a complete example, see Authentication from an ASPX Page.

using (new CrmImpersonator())
    CrmAuthenticationToken token;
    if (offline == true)
        token = new CrmAuthenticationToken();
        token = CrmAuthenticationToken.ExtractCrmAuthenticationToken(Context, orgname);
    token.OrganizationName = orgname;
    token.AuthenticationType = 0;

    //Create the Service
    CrmService service = new CrmService();
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    service.CrmAuthenticationTokenValue = token;
    service.Url = crmurl;

    account account = new account();
    account.name = "Offline Impersonator: " + DateTime.Now.TimeOfDay.ToString();
    if (offline == false)
        account.ownerid = new Owner("systemuser", token.CallerId);



Namespace: Microsoft.Crm.Sdk

Assembly: Microsoft.Crm.Sdk.dll

See Also


© 2010 Microsoft Corporation. All rights reserved.

© 2015 Microsoft