Click here to Skip to main content
12,625,402 members (34,905 online)
Click here to Skip to main content

Stats

24.6K views
1.1K downloads
30 bookmarked
Posted

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
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
library
ICSharpCode.SharpZipLib.dll
Ionic.Zip.dll
bin
Debug
ClientCommonLib.dll
ClientCommonLib.pdb
GlobalCommonLib.dll
GlobalCommonLib.pdb
Release
ClientCommonLib.csprojResolveAssemblyReference.cache
ClientCommonLib.dll
ClientCommonLib.pdb
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
TempPE
Client.exe
Client.pdb
Client.vshost.exe
Client.vshost.exe.manifest
ClientCommonLib.dll
ClientCommonLib.pdb
GlobalCommonLib.dll
GlobalCommonLib.pdb
MessageLog.svclog
ServiceA.Contract.dll
ServiceA.Contract.pdb
ServiceB.Contract.dll
ServiceB.Contract.pdb
Client.csprojResolveAssemblyReference.cache
Client.exe
Client.pdb
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
TempPE
bin
Debug
GlobalCommonLib.dll
GlobalCommonLib.pdb
ICSharpCode.SharpZipLib.dll
Ionic.Zip.dll
Release
GlobalCommonLib.csproj.user
DesignTimeResolveAssemblyReferencesInput.cache
GlobalCommonLib.csprojResolveAssemblyReference.cache
GlobalCommonLib.dll
GlobalCommonLib.pdb
ResolveAssemblyReference.cache
TempPE
RoutingServicePOC.5.1.ReSharper.user
RoutingServicePOC.suo
App_Data
bin
GlobalCommonLib.dll
GlobalCommonLib.pdb
ICSharpCode.SharpZipLib.dll
Ionic.Zip.dll
RoutingServicePOC.dll
RoutingServicePOC.pdb
Database
DesignTimeResolveAssemblyReferencesInput.cache
bin
GlobalCommonLib.dll
GlobalCommonLib.pdb
RoutingServicePOC.dll
RoutingServicePOC.pdb
RoutingService.svc
RoutingServicePOC.zip
ResolveAssemblyReference.cache
RoutingServicePOC.csprojResolveAssemblyReference.cache
RoutingServicePOC.dll
RoutingServicePOC.pdb
TempPE
RoutingService.svc
RoutingServicePOC.csproj.user
bin
Debug
ServiceA.Contract.dll
ServiceA.Contract.pdb
Release
DesignTimeResolveAssemblyReferencesInput.cache
ServiceA.Contract.dll
ServiceA.Contract.pdb
TempPE
GlobalCommonLib.dll
GlobalCommonLib.pdb
ServiceA.Contract.dll
ServiceA.Contract.pdb
ServiceA.exe
ServiceA.pdb
ServiceA.vshost.exe
ServiceA.vshost.exe.manifest
ServiceCommonLib.dll
ServiceCommonLib.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
ServiceA.csprojResolveAssemblyReference.cache
ServiceA.exe
ServiceA.pdb
TempPE
bin
Debug
ServiceB.Contract.dll
ServiceB.Contract.pdb
Release
DesignTimeResolveAssemblyReferencesInput.cache
ServiceB.Contract.dll
ServiceB.Contract.pdb
TempPE
GlobalCommonLib.dll
GlobalCommonLib.pdb
ServiceB.Contract.dll
ServiceB.Contract.pdb
ServiceB.exe
ServiceB.pdb
ServiceB.vshost.exe
ServiceB.vshost.exe.manifest
ServiceCommonLib.dll
ServiceCommonLib.pdb
Release
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
ServiceB.csprojResolveAssemblyReference.cache
ServiceB.exe
ServiceB.pdb
TempPE
bin
Debug
GlobalCommonLib.dll
GlobalCommonLib.pdb
ServiceCommonLib.dll
ServiceCommonLib.pdb
Release
DesignTimeResolveAssemblyReferencesInput.cache
ResolveAssemblyReference.cache
ServiceCommonLib.csprojResolveAssemblyReference.cache
ServiceCommonLib.dll
ServiceCommonLib.pdb
TempPE
.crc
.version
5
6ecd42b4.dat
DecompilerCache
.crc
.version
1
64bd7be7.dat
PdbInfo
ProjectModel.dat
.crc
.version
SymbolCache.bin
.crc
.version
0
1
2
5402198f.dat
6
.crc
.version
5402198f.dat
7
.crc
.version
.crc
.version
3da9581c.dat
3
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 PracticePRO Software Systems Inc
United States United States
"In my childhood, my uncle has shown me how to see the cloud in a close look and I understand that one can draw some elements of the Earth in the sky-canvas if he/she wants to. After that the cloud becomes closer to me and It teaches me one thing that, a deeper-look to something will give you some clues to draw your imagination. You can able to see that one which you have build-up in your mind.

Years past, I have started my career as a software engineer and has been looking for passion in my coding and development which I should be to enjoy my profession and has started asking myself- 'am I doing any engineering here?!' Is my code becoming that thing which I have designed in my mind? So to find that answer I have tried that old solution here... I have decided to come closer to my code and start analyzing them. And it is really working for me and at least it gives me the confidence that I can build something that I really want to. I can draw my thinking there through my code and can build-up my vision that I have designed in my mind. It also helps me to think out of the box, solve each problems by making blocks and make me careful on each steps." ----- says Morshed Anwar.

• Morshed's Technical Blog site: http://morshedanwar.wordpress.com/

• Morshed's Technical articles those are published in Codeproject site: http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=2992452

• Morshed's Linkedin profile: http://www.linkedin.com/in/morshedanwar

• Morshed's Facebook Profile : http://www.facebook.com/morshed.pulok

Beside all these Morshed like to do - photography and music. Here is Morshed's Flickr photos : http://www.flickr.com/photos/morshed_anwar/

You may also be interested in...

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