Skip to content

An HttpModule that reads and writes cookies via watched query string keys

Notifications You must be signed in to change notification settings

BrianSeng/Csharp-Cookie-HttpModule

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 

Repository files navigation

Read/Write Cookie HttpModule

An HttpModule that reads and writes cookies for any watched query string keys coming through the request pipeline. Includes the ability to create an anonymous user cookie that contains a unique ID as well as an example key vault for the retrieval of your watched keys. Also employs a means of inversion of control. Some assembly required ;)

Setting Up

Register service componenets with the UnityConfig container in the App_Start folder:

public static class UnityConfig
{
    private static UnityContainer _container;
    public static UnityContainer GetContainer()
    {
        return _container;
    }
    public static void RegisterComponents(HttpConfiguration config)
    {
        // register all your components with the container here
        // it is NOT necessary to register your controllers
        UnityContainer container = new UnityContainer();

        container.RegisterType<IUserService, UserService>(new ContainerControlledLifetimeManager());
        container.RegisterType<ICookiesService, CookiesService>(new ContainerControlledLifetimeManager());
        container.RegisterType<IConfigService, SiteConfig>(new ContainerControlledLifetimeManager());

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        _container = container;
    }
}

Set up your query string keys to watch & register the http module via Web.config:

<appSettings>
    <add key="WatchedKeys" value="example,example1,example2" />
    <add key="Example.ApiKey" value="example1293jD_nWo.example94keLdm" />
</appSettings>
<modules>
      <add name="CookieModule" type="Example.Namespace.HttpModule.CookieModule" preCondition="managedHandler" />
</modules>

Call the configuration from Global.asax:

protected void Application_Start()
{
    // enable Unity injection - based on example from http://www.asp.net/web-api/overview/advanced/dependency-injection
    UnityConfig.RegisterComponents(System.Web.Http.GlobalConfiguration.Configuration);
}

Alter to relevant namespaces and include proper using statements into the various files.

About

An HttpModule that reads and writes cookies via watched query string keys

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages