Monday, July 30, 2012

Webpart to read passwords from KeePass database

Developed a webpart which gets the data from KeePass database and displays it to the secured users. I wanted to share the code to other users, so that it makes it easy for them develop and they don't have to jump through the same hoops as I did.


I did my business processing in WCF service and the validation and security checks on my webpart.
The gist of the wcf service which is calling the KeePass software to get the details of groups and other information from KeePass is below.


 public class PswdService : IPswdService
    {
        public List<dbVal> GetData(string masterpw, string pswdFilePath {
            try  {
                var ioConnInfo = new IOConnectionInfo { Path = pswdFilePath };
                var compKey = new CompositeKey();
                compKey.AddUserKey(new KcpPassword(masterpw));
                var db = new KeePassLib.PwDatabase();
                db.Open(ioConnInfo, compKey, null);
                List<dbVal> kpdata = (from entry in db.RootGroup.GetEntries(true)
                                      select new dbVal
                                      {
                                          Group = entry.ParentGroup.Name,
                                          ParentGroupPath = entry.ParentGroup.GetFullPath("/", true),
                                          Title = entry.Strings.ReadSafe("Title"),
                                          Username = entry.Strings.ReadSafe("UserName"),
                                          Password = entry.Strings.ReadSafe("Password"),
                                          URL = entry.Strings.ReadSafe("URL"),
                                          Notes = entry.Strings.ReadSafe("Notes")
                                      }).ToList();
                List<dbVal> lstDbVal = kpdata.ToList();
                db.Close();
                return lstDbVal;
            }
            catch (Exception ex)

In my webpart, I am calling the wcf service and based on the user selection, I am showing the results. I have not included the code for user selections, as it is pretty straight forward.


 masterList = new List<GetPasswords.dbVal>();
                GetPasswords.PswdServiceClient wcfClient = new GetPasswords.PswdServiceClient();
                wcfClient.Open();
                masterList = (List<GetPasswords.dbVal>)wcfClient.GetData(masterPswd, parentWebPart.KeePassFilePath);
                wcfClient.Close();
                if (masterList == null)     //Invalid password   
                {
                    MessageLabel.Text = "Invalid Password, no password records returned, Please try again.";
                    return;
                }
                parentWebPart.PswdDetails = null;
                parentWebPart.PswdDetails = new List<GetPasswords.dbVal>(masterList);
                parentWebPart.SaveProperties();
                string[] parentpath;
                List<string> ProductList = new List<string>();
                int count;
                foreach (GetPasswords.dbVal dbValObj in masterList)  {
                    count = dbValObj.ParentGroupPath.Count(f => f == '/');
                    if (count >= 2)
                    {
                        parentpath = dbValObj.ParentGroupPath.Split('/');
                        ProductList.Add(parentpath[PRODUCT]);
                    }
                }
                ProductCombo.DataSource = ProductList.Distinct();
                ProductCombo.DataBind();
                ProductCombo.Items.Insert(0, new ListItem(String.Empty, String.Empty));
                ProductCombo.SelectedIndex = 0;
                MessageLabel.Text = "Please select Product ";



Hope this is helpful for you guys. With KeePass API, you can do a lot more things. If you want to explore more, goto http://keepass.info/index.html and download the tool and there code.

I want to acknowledge the help of Ronnie Overby  for his help on KeePass.