/// <summary> Creates a user. /// /// </summary> /// <param name="accountName">The account name for the user. /// </param> /// <param name="password1">The password for the user. /// </param> /// <param name="password2">A confirmation of the password for the user. /// /// </param> /// <returns> The new User object. /// /// </returns> /// <seealso cref="Owasp.Esapi.Interfaces.IAuthenticator.CreateUser(string, string, string)"> /// </seealso> public IUser CreateUser(string accountName, string password1, string password2) { lock (this) { LoadUsersIfNecessary(); if (accountName == null) { throw new AuthenticationAccountsException("Account creation failed", "Attempt to create user with null accountName"); } if (userMap.Contains(accountName.ToLower())) { throw new AuthenticationAccountsException("Account creation failed", "Duplicate user creation denied for " + accountName); } IUser user = new User(accountName, password1, password2); userMap[accountName.ToLower()] = user; logger.LogCritical(ILogger_Fields.SECURITY, "New user created: " + accountName); SaveUsers(); return user; } }
protected void LoadUsersImmediately() { // file was touched so reload it lock (this) { logger.LogTrace(ILogger_Fields.SECURITY, "Loading users from " + userDB.FullName, null); // FIXME: AAA Necessary? // add the Anonymous user to the database // map.put(anonymous.getAccountName(), anonymous); StreamReader reader = null; try { Hashtable map = new Hashtable(); reader = new StreamReader(userDB.FullName, System.Text.Encoding.Default); string line = null; while ((line = reader.ReadLine()) != null) { if (line.Length > 0 && line[0] != '#') { IUser user = new User(line); if (!user.AccountName.Equals("anonymous")) { if (map.ContainsKey(user.AccountName)) { logger.LogCritical(ILogger_Fields.SECURITY, "Problem in user file. Skipping duplicate user: "******"User file reloaded: " + map.Count, null); } catch (System.Exception e) { logger.LogCritical(ILogger_Fields.SECURITY, "Failure loading user file: " + userDB.FullName, e); } finally { try { if (reader != null) { reader.Close(); } } catch (IOException e) { logger.LogCritical(ILogger_Fields.SECURITY, "Failure closing user file: " + userDB.FullName, e); } } } }
public static void Main(string[] args) { if (args.Length != 3) { System.Console.Out.WriteLine("Usage: Authenticator accountname password role"); return; } Authenticator auth = new Authenticator(); string accountName = args[0].ToLower(); string password = args[1]; string role = args[2]; User user = (User) auth.GetUser(args[0]); if (user == null) { user = new User(); user.AccountName = accountName; auth.userMap[accountName] = user; logger.LogCritical(ILogger_Fields.SECURITY, "New user created: " + accountName); } string newHash = auth.HashPassword(password, accountName); user.SetHashedPassword(newHash); user.AddRole(role); user.Enable(); user.Unlock(); auth.SaveUsers(); long ticks_two = auth.lastModified; long ticks = auth.userDB.LastWriteTime.Ticks; System.Console.Out.WriteLine("User account " + user.AccountName + " updated"); }