Click here to Skip to main content
Click here to Skip to main content

How to Host a KeePass Database in IIS 7.5

, 7 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Create an ASP.NET HTTP handler and configure IIS 7.5 to load and save KeePass databases

Introduction

KeePass is an open source password manager. The passwords are stored in a database with the .kdbx extension. The database can be on your local file system or on a web server.

If you put the database on an IIS webserver, you can access it from KeePass, but if you try to save, you get a 404 error. This is because the StaticFile HTTP handler in IIS only supports the GET method and KeePass requires PUT, MOVE and DELETE, in addition to GET. To add support for this, a custom HTTP handler has to be added to the webserver.

I have tested the solution in IIS 7.5, but it will probably work in IIS 7 as well.

Using the Code

The HTTP handler is a C# library project with a class that implements IHttpHandler. A reference to System.Web needs to be added.

using System.IO;
using System.Linq;
using System.Web; 

public class UploadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string path = Path.Combine(context.Request.PhysicalApplicationPath, 
                      context.Request.Url.Segments.Last());
        
        if (context.Request.RequestType == "DELETE")
        {
            File.Delete(path);
        }
        else if (context.Request.RequestType == "MOVE")
        {
            string destination = Path.Combine(context.Request.PhysicalApplicationPath,
                                 context.Request.Headers["Destination"]
                                 .Split(new char[] { '/' }).Last());
            File.Move(path, destination);
        }
        else if (context.Request.RequestType == "PUT")
        {
            byte[] fileBytes;
            using (var reader = new BinaryReader(context.Request.InputStream))
            {
                fileBytes = reader.ReadBytes((int)context.Request.InputStream.Length);
            }
            File.WriteAllBytes(path, fileBytes);
        }
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

To add the handler, put the following in web.config:

<configuration>
  <system.webserver>
    <staticcontent>
      <mimemap mimetype="application/octet-stream" fileextension=".kdbx">
    </staticcontent>
    <handlers>
      <add name="UploadHandler_tmp" requireaccess="Read" 
      resourcetype="Unspecified" type="UploadHandler" 
      verb="PUT,DELETE,MOVE" path="*.tmp">
      <add name="UploadHandler_kdbx" requireaccess="Read" 
      resourcetype="Unspecified" type="UploadHandler" 
      verb="DELETE" path="*.kdbx">
    </handlers>
  </system.webserver>
</configuration> 

Put the library DLL in a folder named "bin" on the web site.

File system

Now, you should be able to save your KeePass database!

History

  • First version

License

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

Share

About the Author

Johan L.
Software Developer Infracontrol
Sweden Sweden
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberDavid Pierson27-Jun-14 22:20 
Generalpromising Pinmemberbinabic8-Nov-13 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 8 Nov 2013
Article Copyright 2013 by Johan L.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid