Call Custom Service from Event Receiver as Current User

Hello,

i bang my head on this problem for days now:

My custom web service hosted in Sharepoint 2013 needs to know the logged on user name. I used to have a self-written impersonation in my service client before, but with switching to claims authentication this should be obsolete.

So within my service i decode the user from

IClaimsPrincipal icp = Thread.CurrentPrincipal as IClaimsPrincipal;
IClaimsIdentity ci = (IClaimsIdentity)icp.Identity;
String User = ci.ToString();

This works well when i call the service from a custom aspx page.

It utterly fails when i call the service from a List Item Event Receiver. The User always is the Application Pool Account.

This is the Client code to open the Channel:

private void SetChannelFactory(
    MyServiceApplicationProxy proxy,
    Uri address)
{
    if (null == proxy)
    {
        throw new ArgumentNullException("proxy");
    }

    if (null == address)
    {
        throw new ArgumentNullException("address");
    }

    // Check for a cached channel factory            
    string endpointConfigurationName = GetEndpointConfigurationName(address);// Get the endpoint configuration name
    if ((null == s_ChannelFactory) || (endpointConfigurationName != m_EndpointConfigurationName))
    {
        lock (s_ChannelFactoryLock)
        {
            if ((null == s_ChannelFactory) || (endpointConfigurationName != m_EndpointConfigurationName))
            {
                // Create a channel factory without specifying an endpoint address
                // so it can be cached and used for multiple endpoint addresses
                s_ChannelFactory = new ConfigurationChannelFactory<IMyServiceContract>(
                    endpointConfigurationName, proxy.Configuration, null);

                // Configure the channel factory for claims-based authentication
                s_ChannelFactory.ConfigureCredentials(SPServiceAuthenticationMode.Claims);

                foreach (var operation in s_ChannelFactory.Endpoint.Contract.Operations)
                {
                    DataContractSerializerOperationBehavior behavior = operation.Behaviors.Find<DataContractSerializerOperationBehavior>() as DataContractSerializerOperationBehavior;
                    if (behavior != null)
                    {
                        behavior.MaxItemsInObjectGraph = 2147483647;
                    }
                }

                // Store the current endpoint configuration name.
                m_EndpointConfigurationName = endpointConfigurationName;
            }
        }
    }
}

private IMyServiceContract GetChannel(
    MyServiceApplicationProxy proxy,
    Uri address)
{
    //Create Channelfactory
    SetChannelFactory(proxy, address);

    // Create a channel from the channel factory.
    return s_ChannelFactory.CreateChannelActingAsLoggedOnUser(new EndpointAddress(address));
}

All research pointed out that "CreateChannelActingAsLoggedOnUser" would pass the current user (which is correctly identified within the event receiver!) to the service, but it doesn't work for my event receiver...

Any advice on this would be great!

With kind regards,

Joachim



  • Edited by Jo Ott 2 hours 50 minutes ago
February 19th, 2015 4:11am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics