/// <summary>
 /// Gets a list of the roles that a specified user is in for the configured applicationName.
 /// </summary>
 /// <returns>
 /// A string array containing the names of all the roles that the specified user is in for the configured applicationName.
 /// </returns>
 /// <param name="username">The user to return a list of roles for.</param>
 public override string[] GetRolesForUser(string username)
 {
     if (string.IsNullOrEmpty(username)) throw new ArgumentNullException("username");
     //create a new BrightstarDB context using the values in Web.config
     var context = new NerdDinnerContext();
     //find a match for the username
     var login = context.NerdDinnerLogins.Where(l => l.Username.Equals(username)).FirstOrDefault();
     if (login == null) return null;
     //return the Roles collection
     return login.Roles.ToArray();
 }
 /// <summary>
 /// Adds the specified user names to the specified roles for the configured applicationName.
 /// </summary>
 /// <param name="usernames">A string array of user names to be added to the specified roles. </param><param name="roleNames">A string array of the role names to add the specified user names to.</param>
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     //create a new BrightstarDB context using the values in Web.config
     var context = new NerdDinnerContext();
     foreach (var username in usernames)
     {
         //find the match for the username
         var login = context.NerdDinnerLogins.Where(l => l.Username.Equals(username)).FirstOrDefault();
         if (login == null) continue;
         foreach (var role in roleNames)
         {
             //if the Roles collection of the login does not already contain the role, then add it
             if (login.Roles.Contains(role)) continue;
             login.Roles.Add(role);
         }
     }
     context.SaveChanges();
 }
 /// <summary>
 /// Gets a list of users in the specified role for the configured applicationName.
 /// </summary>
 /// <returns>
 /// A string array containing the names of all the users who are members of the specified role for the configured applicationName.
 /// </returns>
 /// <param name="roleName">The name of the role to get the list of users for.</param>
 public override string[] GetUsersInRole(string roleName)
 {
     if (string.IsNullOrEmpty(roleName)) throw new ArgumentNullException("roleName");
     //create a new BrightstarDB context using the values in Web.config
     var context = new NerdDinnerContext();
     //search for all logins who have the supplied roleName in their Roles collection
     var usersInRole = context.NerdDinnerLogins.Where(l => l.Roles.Contains(roleName.ToLower())).Select(l => l.Username).ToList();
     return usersInRole.ToArray();
 }
 /// <summary>
 /// Gets a value indicating whether the specified user is in the specified role for the configured applicationName.
 /// </summary>
 /// <returns>
 /// true if the specified user is in the specified role for the configured applicationName; otherwise, false.
 /// </returns>
 /// <param name="username">The username to search for.</param>
 /// <param name="roleName">The role to search in.</param>
 public override bool IsUserInRole(string username, string roleName)
 {
     try
     {
         //create a new BrightstarDB context using the values in Web.config
         var context = new NerdDinnerContext();
         //find a match for the username
         var login = context.NerdDinnerLogins.Where(l => l.Username.Equals(username)).FirstOrDefault();
         if (login == null || login.IsLockedOut || !login.IsActivated)
         {
             // no match or inactive automatically returns false
             return false;
         }
         //if the Roles collection of the login contains the role we are checking for, return true
         return login.Roles.Contains(roleName.ToLower());
     }
     catch (Exception)
     {
         return false;
     }
 }
 public override bool ValidateUser(string username, string password)
 {
     //Create a context using the connection string set in Web.config
     var context = new NerdDinnerContext();
     //Query the store for a NerdDinnerLogin matching the supplied username
     var logins = context.NerdDinnerLogins.Where(l => l.Username.Equals(username));
     if (logins.Count() == 1)
     {
         //Ensure that only a single login matches the supplied username
         var login = logins.First();
         //Check the properties on the NerdDinnerLogin to ensure the user account is activate and not locked out
         if (login.IsLockedOut || !login.IsActivated) return false;
         //Validate the password of the NerdDinnerLogin against the supplied password
         var validatePassword = login.Password == CreatePasswordHash(password, login.PasswordSalt);
         if (!validatePassword)
         {
             //return validation failure
             return false;
         }
         //return validation success
         return true;
     }
     return false;
 }
 public override string GetUserNameByEmail(string email)
 {
     if (string.IsNullOrEmpty(email)) return "";
     //Create a context using the connection string in Web.config
     var context = new NerdDinnerContext();
     //Query the store for a NerdDinnerLogin that matches the supplied username
     var login = context.NerdDinnerLogins.Where(l => l.Email.Equals(email)).FirstOrDefault();
     if (login == null) return string.Empty;
     return login.Username;
 }
 public override MembershipUser GetUser(string username, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(username)) return null;
     //Create a context using the connection string in Web.config
     var context = new NerdDinnerContext();
     //Query the store for a NerdDinnerLogin that matches the supplied username
     var login = context.NerdDinnerLogins.Where(l => l.Username.Equals(username)).FirstOrDefault();
     if (login == null) return null;
     if(userIsOnline)
     {
         //if the call states that the user is online, update the LastActive property of the NerdDinnerLogin
         login.LastActive = DateTime.UtcNow;
         context.SaveChanges();
     }
     return ConvertLoginToMembershipUser(login);
 }
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            var args = new ValidatePasswordEventArgs(email, password, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (string.IsNullOrEmpty(email))
            {
                status = MembershipCreateStatus.InvalidEmail;
                return null;
            }

            if (string.IsNullOrEmpty(password))
            {
                status = MembershipCreateStatus.InvalidPassword;
                return null;
            }

            if (RequiresUniqueEmail && GetUserNameByEmail(email) != "")
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return null;
            }

            var u = GetUser(username, false);

            try
            {
                if (u == null)
                {
                    var salt = CreateSalt();
                    
                    //Create a new NerdDinnerLogin entity and set the properties
                    var login = new NerdDinnerLogin
                    {
                        Username = username,
                        Email = email,
                        PasswordSalt = salt,
                        Password = CreatePasswordHash(password, salt),
                        CreatedDate = DateTime.UtcNow,
                        IsActivated = true,
                        IsLockedOut = false,
                        LastLockedOutDate = DateTime.UtcNow,
                        LastLoginDate = DateTime.UtcNow,
                        LastActive = DateTime.UtcNow
                    };
                    //Create a context using the connection string in the Web.Config
                    var context = new NerdDinnerContext();
                    //Add the entity to the context
                    context.NerdDinnerLogins.Add(login);
                    //Save the changes to the BrightstarDB store
                    context.SaveChanges();

                    status = MembershipCreateStatus.Success;
                    return GetUser(username, true /*online*/);
                }
            }
            catch (Exception)
            {
                status = MembershipCreateStatus.ProviderError;
                return null;
            }

            status = MembershipCreateStatus.DuplicateUserName;
            return null;
        }