Index: /branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs
===================================================================
--- /branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs	(revision 3957)
+++ /branches/HeuristicLab.Services.Authentication Prototype/Service/Provider/HeuristicLabMembershipProvider.cs	(revision 3958)
@@ -1,3 +1,5 @@
 ﻿using System;
+using System.Collections.Specialized;
+using System.Configuration;
 using System.Configuration.Provider;
 using System.Linq;
@@ -11,21 +13,77 @@
   class HeuristicLabMembershipProvider : MembershipProvider {
 
-      private string pApplicationName;
-      private bool pEnablePasswordReset;
-      private bool pEnablePasswordRetrieval;
-      private bool pRequiresQuestionAndAnswer;
-      private bool pRequiresUniqueEmail;
-      private int pMaxInvalidPasswordAttempts;
-      private int pPasswordAttemptWindow;
-      private MembershipPasswordFormat pPasswordFormat;
-      private MachineKeySection machineKey;
-
-      public override string ApplicationName {
-      get {
-        throw new NotImplementedException();
-      }
-      set {
-        throw new NotImplementedException();
-      }
+    private string pApplicationName;
+    private bool pEnablePasswordReset;
+    private bool pEnablePasswordRetrieval;
+    private bool pRequiresQuestionAndAnswer;
+    private bool pRequiresUniqueEmail;
+    private int pMaxInvalidPasswordAttempts;
+    private int pPasswordAttemptWindow;
+    private int pMinRequiredPasswordLength;
+    private MembershipPasswordFormat pPasswordFormat;
+    private MachineKeySection machineKey;
+
+
+    public override void Initialize(string name, NameValueCollection config) {
+      //
+      // Initialize values from web.config.
+      //
+
+      if (config == null)
+        throw new ArgumentNullException("config");
+
+      if (name == null || name.Length == 0)
+        name = "HeuristicLabMembershipProvider";
+
+      if (String.IsNullOrEmpty(config["description"])) {
+        config.Remove("description");
+        config.Add("description", "Heuristic Lab Membership provider");
+      }
+
+      // Initialize the abstract base class.
+      base.Initialize(name, config);
+
+      pApplicationName = GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
+      pMaxInvalidPasswordAttempts = Convert.ToInt32(GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
+      pPasswordAttemptWindow = Convert.ToInt32(GetConfigValue(config["passwordAttemptWindow"], "10"));
+      pMinRequiredPasswordLength = Convert.ToInt32(GetConfigValue(config["minRequiredPasswordLength"], "7"));
+      pEnablePasswordReset = Convert.ToBoolean(GetConfigValue(config["enablePasswordReset"], "true"));
+      pEnablePasswordRetrieval = Convert.ToBoolean(GetConfigValue(config["enablePasswordRetrieval"], "true"));
+      pRequiresQuestionAndAnswer = Convert.ToBoolean(GetConfigValue(config["requiresQuestionAndAnswer"], "false"));
+      pRequiresUniqueEmail = Convert.ToBoolean(GetConfigValue(config["requiresUniqueEmail"], "true"));
+
+      string tempFormat = config["passwordFormat"];
+      if (tempFormat == null) {
+        tempFormat = "Hashed";
+      }
+
+      switch (tempFormat) {
+        case "Hashed":
+          pPasswordFormat = MembershipPasswordFormat.Hashed;
+          break;
+        case "Encrypted":
+          pPasswordFormat = MembershipPasswordFormat.Encrypted;
+          break;
+        case "Clear":
+          pPasswordFormat = MembershipPasswordFormat.Clear;
+          break;
+        default:
+          throw new ProviderException("Password format not supported.");
+      }
+
+      // Get encryption and decryption key information from the configuration.
+      Configuration cfg =
+        WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
+      machineKey = (MachineKeySection)cfg.GetSection("system.web/machineKey");
+
+      if (machineKey.ValidationKey.Contains("AutoGenerate"))
+        if (PasswordFormat != MembershipPasswordFormat.Clear)
+          throw new ProviderException("Hashed or Encrypted passwords " +
+                                      "are not supported with auto-generated keys.");
+    }
+
+    public override string ApplicationName {
+      get { return pApplicationName; }
+      set { pApplicationName = value; }
     }
 
@@ -122,5 +180,5 @@
         try {
           // try to get user
-          HeuristicLabUser u = 
+          HeuristicLabUser u =
             db.HeuristicLabUsers.Single<HeuristicLabUser>(x => x.UserName == username);
 
@@ -129,5 +187,5 @@
             db.HeuristicLabUserRole.DeleteAllOnSubmit<HeuristicLabUserRole>(u.HeuristicLabUserRole);
           }
-          
+
           // delete user
           db.HeuristicLabUsers.DeleteOnSubmit(u);
@@ -231,19 +289,16 @@
     /// <param name="password"></param>
     /// <returns></returns>
-    public override bool ValidateUser(string username, string password)
-    {
-        bool isValid = false;
-        using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext())
-        {
-            if (db == null)
-            {
-                return false;
-            }
-            HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
-            isValid = CheckPassword(password, u.GetPassword());
-        }
-        return isValid;
-    }
-    
+    public override bool ValidateUser(string username, string password) {
+      bool isValid = false;
+      using (DataClassesDataContext db = DatabaseUtil.createDataClassesDataContext()) {
+        if (db == null) {
+          return false;
+        }
+        HeuristicLabUser u = db.HeuristicLabUsers.Single(x => x.UserName == username);
+        isValid = CheckPassword(password, u.GetPassword());
+      }
+      return isValid;
+    }
+
     /// <summary>
     /// compaiers to passwords
@@ -252,58 +307,53 @@
     /// <param name="dbpassword"></param>
     /// <returns></returns>
-    private bool CheckPassword(string password, string dbpassword)
-    {
-        string pass1 = password;
-        string pass2 = dbpassword;
-
-        switch (PasswordFormat)
-        {
-            case MembershipPasswordFormat.Encrypted:
-                pass2 = DecodePassword(dbpassword);
-                break;
-            case MembershipPasswordFormat.Hashed:
-                pass1 = EncodePassword(password);
-                break;
-            default:
-                break;
-        }
-
-        if (pass1 == pass2)
-        {
-            return true;
-        }
-
-        return false;
-    }
-
-
-   /// <summary>
-   /// Encodes a password
-   /// </summary>
-   /// <param name="password"></param>
-   /// <returns></returns>
-   private string EncodePassword(string password)
-    {
-        string encodedPassword = password;
-
-        switch (PasswordFormat)
-        {
-            case MembershipPasswordFormat.Clear:
-                break;
-            case MembershipPasswordFormat.Encrypted:
-                encodedPassword =
-                  Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
-                break;
-            case MembershipPasswordFormat.Hashed:
-                HMACSHA1 hash = new HMACSHA1();
-                hash.Key = HexToByte(machineKey.ValidationKey);
-                encodedPassword =
-                  Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
-                break;
-            default:
-                throw new ProviderException("Unsupported password format.");
-        }
-
-        return encodedPassword;
+    private bool CheckPassword(string password, string dbpassword) {
+      string pass1 = password;
+      string pass2 = dbpassword;
+
+      switch (PasswordFormat) {
+        case MembershipPasswordFormat.Encrypted:
+          pass2 = DecodePassword(dbpassword);
+          break;
+        case MembershipPasswordFormat.Hashed:
+          pass1 = EncodePassword(password);
+          break;
+        default:
+          break;
+      }
+
+      if (pass1 == pass2) {
+        return true;
+      }
+
+      return false;
+    }
+
+
+    /// <summary>
+    /// Encodes a password
+    /// </summary>
+    /// <param name="password"></param>
+    /// <returns></returns>
+    private string EncodePassword(string password) {
+      string encodedPassword = password;
+
+      switch (PasswordFormat) {
+        case MembershipPasswordFormat.Clear:
+          break;
+        case MembershipPasswordFormat.Encrypted:
+          encodedPassword =
+            Convert.ToBase64String(EncryptPassword(Encoding.Unicode.GetBytes(password)));
+          break;
+        case MembershipPasswordFormat.Hashed:
+          HMACSHA1 hash = new HMACSHA1();
+          hash.Key = HexToByte(machineKey.ValidationKey);
+          encodedPassword =
+            Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
+          break;
+        default:
+          throw new ProviderException("Unsupported password format.");
+      }
+
+      return encodedPassword;
     }
 
@@ -314,23 +364,21 @@
     /// <param name="encodedPassword"></param>
     /// <returns></returns>
-    private string DecodePassword(string encodedPassword)
-    {
-        string password = encodedPassword;
-
-        switch (PasswordFormat)
-        {
-            case MembershipPasswordFormat.Clear:
-                break;
-            case MembershipPasswordFormat.Encrypted:
-                password =
-                  Encoding.Unicode.GetString(DecryptPassword(Convert.FromBase64String(password)));
-                break;
-            case MembershipPasswordFormat.Hashed:
-                throw new ProviderException("Cannot unencode a hashed password.");
-            default:
-                throw new ProviderException("Unsupported password format.");
-        }
-
-        return password;
+    private string DecodePassword(string encodedPassword) {
+      string password = encodedPassword;
+
+      switch (PasswordFormat) {
+        case MembershipPasswordFormat.Clear:
+          break;
+        case MembershipPasswordFormat.Encrypted:
+          password =
+            Encoding.Unicode.GetString(DecryptPassword(Convert.FromBase64String(password)));
+          break;
+        case MembershipPasswordFormat.Hashed:
+          throw new ProviderException("Cannot unencode a hashed password.");
+        default:
+          throw new ProviderException("Unsupported password format.");
+      }
+
+      return password;
     }
 
@@ -340,10 +388,22 @@
     /// <param name="hexString"></param>
     /// <returns></returns>
-    private static byte[] HexToByte(string hexString)
-    {
-        byte[] returnBytes = new byte[hexString.Length / 2];
-        for (int i = 0; i < returnBytes.Length; i++)
-            returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
-        return returnBytes;
+    private static byte[] HexToByte(string hexString) {
+      byte[] returnBytes = new byte[hexString.Length / 2];
+      for (int i = 0; i < returnBytes.Length; i++)
+        returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
+      return returnBytes;
+    }
+
+    /// <summary>
+    /// returns the configuration string, if the value is null or empty the default value is returned 
+    /// </summary>
+    /// <param name="configValue"></param>
+    /// <param name="defaultValue"></param>
+    /// <returns></returns>
+    private string GetConfigValue(string configValue, string defaultValue) {
+      if (String.IsNullOrEmpty(configValue))
+        return defaultValue;
+
+      return configValue;
     }
   }
