Click here to Skip to main content
15,880,891 members
Articles / Web Development / ASP.NET
Tip/Trick

How to Host a KeePass Database in IIS 7.5

Rate me:
Please Sign up or sign in to vote.
4.93/5 (6 votes)
7 Nov 2013CPOL 21.3K   166   8   3
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.

C#
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:

XML
<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)


Written By
Software Developer Infracontrol
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCouple issues for noobs like me (But this article is good! Thanks!) Pin
Tomas Ramirez30-Apr-15 21:22
Tomas Ramirez30-Apr-15 21:22 
GeneralMy vote of 5 Pin
David Pierson27-Jun-14 22:20
David Pierson27-Jun-14 22:20 
Generalpromising Pin
binabic8-Nov-13 3:24
binabic8-Nov-13 3:24 

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

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