using System;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Adp.Shared.Security;
namespace Adp.Shared.ServiceClient
{
public static class ServiceClientBaseHeaderInjectingExtension
{
///
/// Sets the OID and AOID headers and calls the service.
/// First argument to action is OID
///
public static TResult CallServiceSettingHeaders(
this ClientBase serviceClient,
IPrincipal user,
Func action) where TService : class
{
using (var scope = new OperationContextScope(serviceClient.InnerChannel))
{
AddHeaders(user);
return action(user.Oid());
}
}
///
/// Sets the OID and AOID headers and calls the service.
/// First argument to action is OID and the second in AOID
///
public static TResult CallServiceSettingHeaders(
this ClientBase serviceClient,
IPrincipal user,
Func action) where TService : class
{
using (var scope = new OperationContextScope(serviceClient.InnerChannel))
{
AddHeaders(user);
return action(user.Oid(), user.Aoid());
}
}
///
/// Sets the OID and AOID headers and calls the service.
/// First argument to action is OID and the second in AOID
/// Third argument is passed through
///
public static TResult CallServiceSettingHeaders(
this ClientBase serviceClient,
IPrincipal user,
Func action,
TArg arg) where TService : class
{
using (var scope = new OperationContextScope(serviceClient.InnerChannel))
{
AddHeaders(user);
return action(user.Oid(), user.Aoid(), arg);
}
}
private static void AddHeaders(IPrincipal user)
{
var headers = new HttpRequestMessageProperty();
headers.Headers.Add("oid", user.Oid());
headers.Headers.Add("aoid", user.Aoid());
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = headers;
}
}
}