Click here to Skip to main content
11,431,829 members (62,895 online)
Click here to Skip to main content
Add your own
alternative version

Custom Authentication and Security for Routing Service of WCF 4.0

, 13 Oct 2012 CPOL
Defining security contextes in Routing service environment.
Routing_Service_POC-noexe.zip
Routing_Service_POC
POC
RoutingServicePOC
ClientCommonLib
obj
Debug
Properties
Client
bin
Debug
Client.vshost.exe.manifest
MessageLog.svclog
obj
x86
Debug
Properties
GlobalCommonLib
GlobalCommonLib.csproj.user
obj
Debug
Properties
RoutingServicePOC.5.1.ReSharper.user
RoutingServicePOC.suo
RoutingServicePOC
Filter
obj
Debug
CSAutoParameterize
original
transformed
Package
PackageTmp
RoutingService.svc
RoutingServicePOC.zip
TransformWebConfig
original
transformed
Properties
RoutingService.svc
RoutingServicePOC.csproj.user
ServiceA.Contract
obj
Debug
Properties
ServiceA
bin
Debug
ServiceA.vshost.exe.manifest
obj
x86
Debug
Properties
ServiceB.Contract
obj
Debug
Properties
ServiceB
bin
Debug
ServiceB.vshost.exe.manifest
obj
x86
Debug
Properties
ServiceCommonLib
obj
Debug
Properties
_ReSharper.RoutingServicePOC
BuildScriptCache
.crc
.version
9
6ecd42b4.dat
NamedArguments
.crc
.version
8
64bd7be7.dat
ProjectModel
ProjectModel.dat
Resources
.crc
.version
SymbolCache.bin
TagPrefixes
.crc
.version
5
5402198f.dat
TodoCache
.crc
.version
5
5402198f.dat
WebsiteFileReferences
.crc
.version
WordIndex
.crc
.version
1
3da9581c.dat
Routing_Service_POC.zip
library
ICSharpCode.SharpZipLib.dll
Ionic.Zip.dll
bin
Debug
ClientCommonLib.dll
GlobalCommonLib.dll
ClientCommonLib.dll
Client.exe
Client.vshost.exe
Client.vshost.exe.manifest
ClientCommonLib.dll
GlobalCommonLib.dll
MessageLog.svclog
ServiceA.Contract.dll
ServiceB.Contract.dll
Client.exe
bin
Debug
GlobalCommonLib.dll
ICSharpCode.SharpZipLib.dll
Ionic.Zip.dll
GlobalCommonLib.csproj.user
GlobalCommonLib.dll
RoutingServicePOC.5.1.ReSharper.user
RoutingServicePOC.suo
bin
GlobalCommonLib.dll
ICSharpCode.SharpZipLib.dll
Ionic.Zip.dll
RoutingServicePOC.dll
bin
GlobalCommonLib.dll
RoutingServicePOC.dll
RoutingService.svc
RoutingServicePOC.zip
RoutingServicePOC.dll
RoutingService.svc
RoutingServicePOC.csproj.user
bin
Debug
ServiceA.Contract.dll
ServiceA.Contract.dll
GlobalCommonLib.dll
ServiceA.Contract.dll
ServiceA.exe
ServiceA.vshost.exe
ServiceA.vshost.exe.manifest
ServiceCommonLib.dll
ServiceA.exe
bin
Debug
ServiceB.Contract.dll
ServiceB.Contract.dll
GlobalCommonLib.dll
ServiceB.Contract.dll
ServiceB.exe
ServiceB.vshost.exe
ServiceB.vshost.exe.manifest
ServiceCommonLib.dll
ServiceB.exe
bin
Debug
GlobalCommonLib.dll
ServiceCommonLib.dll
ServiceCommonLib.dll
.crc
.version
6ecd42b4.dat
.crc
.version
64bd7be7.dat
ProjectModel.dat
.crc
.version
SymbolCache.bin
.crc
.version
5402198f.dat
.crc
.version
5402198f.dat
.crc
.version
.crc
.version
3da9581c.dat
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;
using GlobalCommonLib;

namespace ServiceCommonLib
{
    public class CentralSessionDataContext : IExtension<OperationContext>
    {

        //The "current" custom context
        public static CentralSessionDataContext Current
        {
            get { return OperationContext.Current.Extensions.Find<CentralSessionDataContext>(); }
        }

        #region IExtension<OperationContext> Members


        public void Attach(OperationContext owner)
        {
            //no-op
        }

        public void Detach(OperationContext owner)
        {
            //no-op
        }

        #endregion

        //You can have lots more of these -- this is the stuff that you
        //want to store on your custom context
        public CentralRequestSession RequestSession { get; set; }
    }

    public class CentralSessionDataContextBehaviorAttribute : Attribute, IServiceBehavior, IDispatchMessageInspector
    {
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request,
                                          IClientChannel channel,
                                          InstanceContext instanceContext)
        {
            var ctx = new CentralSessionDataContext();
            OperationContext.Current.Extensions.Add(ctx);
            return request.Headers.MessageId;
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            OperationContext.Current.Extensions.Remove(CentralSessionDataContext.Current);
        }

        #region IServiceBehavior Members


        public void AddBindingParameters(ServiceDescription serviceDescription,
                                         ServiceHostBase serviceHostBase,
                                         System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
                                         BindingParameterCollection bindingParameters)
        {
            //no-op
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.MessageInspectors.Add(this);
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            //no-op           
        }

        #endregion
    }

    public class CentralSessionDataContextProcessInspector : BehaviorExtensionElement, IEndpointBehavior, IDispatchMessageInspector
    {
        
        #region Overrides of BehaviorExtensionElement

        /// <summary>
        /// Creates a behavior extension based on the current configuration settings.
        /// </summary>
        /// <returns>
        /// The behavior extension.
        /// </returns>
        protected override object CreateBehavior()
        {
            return new CentralSessionDataContextProcessInspector();
        }

        /// <summary>
        /// Gets the type of behavior.
        /// </summary>
        /// <returns>
        /// B <see cref="T:System.Type"/>.
        /// </returns>
        public override Type BehaviorType
        {
            get { return typeof(CentralSessionDataContextProcessInspector); }
        }

        #endregion

        #region Implementation of IEndpointBehavior

        /// <summary>
        /// Implement to confirm that the endpoint meets some intended criteria.
        /// </summary>
        /// <param name="endpoint">The endpoint to validate.</param>
        public void Validate(ServiceEndpoint endpoint)
        {
        }

        /// <summary>
        /// Implement to pass data at runtime to bindings to support custom behavior.
        /// </summary>
        /// <param name="endpoint">The endpoint to modify.</param><param name="bindingParameters">The objects that binding elements require to support the behavior.</param>
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {

        }

        /// <summary>
        /// Implements a modification or extension of the service across an endpoint.
        /// </summary>
        /// <param name="endpoint">The endpoint that exposes the contract.</param><param name="endpointDispatcher">The endpoint dispatcher to be modified or extended.</param>
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
        }

        /// <summary>
        /// Implements a modification or extension of the client across an endpoint.
        /// </summary>
        /// <param name="endpoint">The endpoint that is to be customized.</param><param name="clientRuntime">The client runtime to be customized.</param>
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {

        }

        #endregion

        #region Implementation of IDispatchMessageInspector

        /// <summary>
        /// Called after an inbound message has been received but before the message is dispatched to the intended operation.
        /// </summary>
        /// <returns>
        /// The object used to correlate state. This object is passed back in the <see cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply(System.ServiceModel.Channels.Message@,System.Object)"/> method.
        /// </returns>
        /// <param name="request">The request message.</param><param name="channel">The incoming channel.</param><param name="instanceContext">The current service instance.</param>
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            CentralSessionDataContext.Current.RequestSession = CredentialHelper.GetSessionData(request);
            return true;
        }

        /// <summary>
        /// Called after the operation has returned but before the reply message is sent.
        /// </summary>
        /// <param name="reply">The reply message. This value is null if the operation is one way.</param><param name="correlationState">The correlation object returned from the <see cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.AfterReceiveRequest(System.ServiceModel.Channels.Message@,System.ServiceModel.IClientChannel,System.ServiceModel.InstanceContext)"/> method.</param>
        public void BeforeSendReply(ref Message reply, object correlationState)
        {

        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Morshed Anwar
Team Leader Adaptive Enterprise Limited (www.ael-bd.com)
Bangladesh Bangladesh
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150428.2 | Last Updated 13 Oct 2012
Article Copyright 2012 by Morshed Anwar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid