Within the Plugin Context, there are two user ids,
InitiatingUserId
which returns the id of the user who actually fired the plugin.UserId
which returns the user Id of the user the plugin is actually running under. (this is the user specified when registering the plugin, or the Calling User, if it's registered to run as the calling user)But I'm interested in a third user id, the impersonated user id of the OrganizationServiceProxy that performed the call.
Lets say I have a ASP.Net website that is running with a CRM System Admin AD account, and any calls that are made to CRM, use impersonation, looking up the CRM userid of the person currently logged into the site.
This works great for selects and updates, the user is only able to see\update what they have rights to. But... if I create a plugin that is is triggered on the update of a particular entity, how do I lookup the user id of the impersonated user on the Asp website from within the plugin?
We can impersonate any user in plugin code by passing user GUID to CreateOrganizationService of IOrganizationServiceFactory. Below is sample plugin code. The code is impersonating with user GUID and then creating task.
Impersonation in plug-ins is not supported while in offline mode.
To impersonate a user, set the CallerId property on an instance of OrganizationServiceProxy before calling the service's Web methods.
As far as my understanding of the situation goes, you are able to retrieve the User Id of the impersonated user, but not the authenticated user. Below are some scenarios mapped out that should hopefully clarify the behavior (tested in UR11 and UR12):
ImpersonatingUserId
of plugin step is set to null
(from the plugin registration tool, "Run in User's Context" would be set to "Calling User)). Example would be with OrganizationServiceProxy
authenticated as User A, where proxyService.CallerId
is User B.
PluginContext.UserId
= User B
PluginContext.InitiatingUserId
= User B
createdonbehalfby
(or modifiedonbehalfby
) = User A
OrganizationServiceProxy
authenticated as User A, where proxyService.CallerId
is User B.
PluginContext.UserId
= User C
PluginContext.InitiatingUserId
= User B
createdonbehalfby
(or modifiedonbehalfby
) = User A
Scenario #1 is a bit confusing to me as I vaguely remember that working differently. This must mean that when the CallerId
property is set, the impersonation happens in a different location than with Scenario #2. However, you can see that in Scenario #2, we seem to achieve your expected result.
The steps I used to produce this result were:
Microsoft.Xrm.Sdk
and Microsoft.Crm.Sdk.Proxy
. Instantiate a new OrganizationServiceProxy
object where the authenticated user is User A. Set the CallerId
to be the Guid representing User B.Create a super simple plugin that just throws an exception the first chance it gets like this:
throw new Exception(string.Format("Initiating Id: {0}, User Id: {1}",
context.InitiatingUserId,
context.UserId));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With