Click here to Skip to main content
11,583,555 members (64,029 online)
Click here to Skip to main content

How to Host a KeePass Database in IIS 7.5

, 7 Nov 2013 CPOL 6.9K 75 7
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

You may also be interested in...

Comments and Discussions

 
GeneralCouple issues for noobs like me (But this article is good! Thanks!) Pin
Tomas Ramirez30-Apr-15 21:22
memberTomas Ramirez30-Apr-15 21:22 
GeneralMy vote of 5 Pin
David Pierson27-Jun-14 22:20
memberDavid Pierson27-Jun-14 22:20 
Generalpromising Pin
binabic8-Nov-13 3:24
memberbinabic8-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 | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 8 Nov 2013
Article Copyright 2013 by Johan L.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid