/// <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();
 }
 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;
        }