Esempio n. 1
0
        private List <Settings> InitSettings()
        {
            WebSecurity.Register("Demo", "123456", "*****@*****.**", true, "Demo", "Demo");
            Roles.CreateRole("Admin");
            Roles.AddUserToRole("Demo", "Admin");

            var settings = new List <Settings>
            {
                new Settings {
                    Key = "UploadPath", Value = "/Content/img/", Title = "Upload Path", Type = "text"
                },
                new Settings {
                    Key = "MaxImageWidth", Value = "1200", Title = "Max Image Width", Type = "text"
                },
                new Settings {
                    Key = "MaxImageHeight", Value = "1200", Title = "Max Image Height", Type = "text"
                },
                new Settings {
                    Key = "UseAzureStorage", Value = "1", Title = "Use Azure Storage", Type = "text"
                },
                new Settings {
                    Key = "AzureAccountName", Value = "kimjansen", Title = "Azure Storage Account Name", Type = "text"
                },
                new Settings {
                    Key = "AzureStorageKey", Value = "hgCJEbpepdnV52CUMeJAWh2U2ViOp5Dkv0WOmbcjz2MO4Xixz7iqMS42QGieltop2NTXnoButt3mgEaX3IYcHw==", Title = "Azure Storage Key", Type = "text"
                },
            };

            settings.ForEach(s => db.Settings.Add(s));

            var categories = new List <Category>
            {
                new Category {
                    Name = "Newborn"
                },
                new Category {
                    Name = "Toddler"
                },
                new Category {
                    Name = "Kids"
                },
                new Category {
                    Name = "Family"
                },
                new Category {
                    Name = "Graduation"
                },
                new Category {
                    Name = "Wedding"
                },
            };

            categories.ForEach(s => db.Categories.Add(s));


            db.SaveChanges();

            return(settings);
        }
Esempio n. 2
0
 public override bool ChangePassword(string username, string oldPassword, string newPassword)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(oldPassword))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(newPassword))
     {
         return(false);
     }
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return(false);
         }
         String  HashedPassword        = User.Password;
         Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, oldPassword));
         if (VerificationSucceeded)
         {
             User.PasswordFailuresSinceLastSuccess = 0;
         }
         else
         {
             int Failures = User.PasswordFailuresSinceLastSuccess;
             if (Failures < MaxInvalidPasswordAttempts)
             {
                 User.PasswordFailuresSinceLastSuccess += 1;
                 User.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             else if (Failures >= MaxInvalidPasswordAttempts)
             {
                 User.LastPasswordFailureDate = DateTime.UtcNow;
                 User.LastLockoutDate         = DateTime.UtcNow;
                 User.IsLockedOut             = true;
             }
             Context.SaveChanges();
             return(false);
         }
         String NewHashedPassword = Crypto.HashPassword(newPassword);
         if (NewHashedPassword.Length > 128)
         {
             return(false);
         }
         User.Password = NewHashedPassword;
         User.LastPasswordChangedDate = DateTime.UtcNow;
         Context.SaveChanges();
         return(true);
     }
 }
Esempio n. 3
0
    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        if (providerUserKey is Guid)
        {
        }
        else
        {
            return(null);
        }

        using (ApertureDataContext Context = new ApertureDataContext())
        {
            User User = null;
            User = Context.Users.Find(providerUserKey);
            if (User != null)
            {
                if (userIsOnline)
                {
                    User.LastActivityDate = DateTime.UtcNow;
                    Context.SaveChanges();
                }
                return(new MembershipUser(Membership.Provider.Name, User.Username, User.UserId, User.Email, null, null, User.IsApproved, User.IsLockedOut, User.CreateDate.Value, User.LastLoginDate.Value, User.LastActivityDate.Value, User.LastPasswordChangedDate.Value, User.LastLockoutDate.Value));
            }
            else
            {
                return(null);
            }
        }
    }
Esempio n. 4
0
 public override MembershipUser GetUser(string username, bool userIsOnline)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(null);
     }
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             if (userIsOnline)
             {
                 User.LastActivityDate = DateTime.UtcNow;
                 Context.SaveChanges();
             }
             return(new MembershipUser(Membership.Provider.Name, User.Username, User.UserId, User.Email, null, null, User.IsApproved, User.IsLockedOut, User.CreateDate.Value, User.LastLoginDate.Value, User.LastActivityDate.Value, User.LastPasswordChangedDate.Value, User.LastLockoutDate.Value));
         }
         else
         {
             return(null);
         }
     }
 }
Esempio n. 5
0
 public override bool ValidateUser(string username, string password)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     if (string.IsNullOrEmpty(password))
     {
         return(false);
     }
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User == null)
         {
             return(false);
         }
         if (!User.IsApproved)
         {
             return(false);
         }
         if (User.IsLockedOut)
         {
             return(false);
         }
         String  HashedPassword        = User.Password;
         Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, password));
         if (VerificationSucceeded)
         {
             User.PasswordFailuresSinceLastSuccess = 0;
             User.LastLoginDate    = DateTime.UtcNow;
             User.LastActivityDate = DateTime.UtcNow;
         }
         else
         {
             int Failures = User.PasswordFailuresSinceLastSuccess;
             if (Failures < MaxInvalidPasswordAttempts)
             {
                 User.PasswordFailuresSinceLastSuccess += 1;
                 User.LastPasswordFailureDate           = DateTime.UtcNow;
             }
             else if (Failures >= MaxInvalidPasswordAttempts)
             {
                 User.LastPasswordFailureDate = DateTime.UtcNow;
                 User.LastLockoutDate         = DateTime.UtcNow;
                 User.IsLockedOut             = true;
             }
         }
         Context.SaveChanges();
         if (VerificationSucceeded)
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
 public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
 {
     if (string.IsNullOrEmpty(roleName))
     {
         return(false);
     }
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         Role Role = null;
         Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
         if (Role == null)
         {
             return(false);
         }
         if (throwOnPopulatedRole)
         {
             if (Role.Users.Any())
             {
                 return(false);
             }
         }
         else
         {
             Role.Users.Clear();
         }
         Context.Roles.Remove(Role);
         Context.SaveChanges();
         return(true);
     }
 }
Esempio n. 7
0
    public string CreateAccount(string userName, string password, bool requireConfirmationToken)
    {
        if (string.IsNullOrEmpty(userName))
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
        }

        if (string.IsNullOrEmpty(password))
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
        }

        string hashedPassword = Crypto.HashPassword(password);

        if (hashedPassword.Length > 128)
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.InvalidPassword);
        }

        using (ApertureDataContext Context = new ApertureDataContext())
        {
            if (Context.Users.Where(Usr => Usr.Username == userName).Any())
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName);
            }

            string token = string.Empty;
            if (requireConfirmationToken)
            {
                token = GenerateToken();
            }

            User NewUser = new User
            {
                UserId     = Guid.NewGuid(),
                Username   = userName,
                Password   = hashedPassword,
                IsApproved = !requireConfirmationToken,
                Email      = string.Empty,
                CreateDate = DateTime.UtcNow,
                LastPasswordChangedDate          = DateTime.UtcNow,
                PasswordFailuresSinceLastSuccess = 0,
                LastLoginDate           = DateTime.UtcNow,
                LastActivityDate        = DateTime.UtcNow,
                LastLockoutDate         = DateTime.UtcNow,
                IsLockedOut             = false,
                LastPasswordFailureDate = DateTime.UtcNow,
                ConfirmationToken       = token
            };

            Context.Users.Add(NewUser);
            Context.SaveChanges();
            return(token);
        }
    }
Esempio n. 8
0
        public ActionResult SaveFiles(string qqfile)
        {
            HttpPostedFileBase          file     = Request.Files[0];
            Dictionary <string, string> versions = new Dictionary <string, string>();

            try
            {
                //Define the versions to generate
                versions.Add("_thumb", "width=" + 100 + "&height=" + 100 + "&crop=auto&format=jpg"); //Crop to square thumbnail
                versions.Add("_medium", "maxwidth=400&maxheight=400&format=jpg");                    //Fit inside 400x400 area, jpeg
                versions.Add("_large", "maxwidth=1900&maxheight=1900&format=jpg");                   //Fit inside 1900x1200 area
                using (var db = new ApertureDataContext())
                {
                    if (file.ContentLength > 0)
                    { //Skip unused file controls.
                        string fileName  = Path.GetFileNameWithoutExtension(file.FileName);
                        string extension = Path.GetExtension(file.FileName);


                        //Generate each version
                        foreach (string suffix in versions.Keys)
                        {
                            UploadFilesToStorageAccount(file, fileName + suffix, versions[suffix]);
                        }

                        var photo = new Photo()
                        {
                            PhotoUrl       = SiteSettings.UploadPath + fileName + "_large.jpg",
                            ThumbnailUrl   = SiteSettings.UploadPath + fileName + "_thumb.jpg",
                            MediumPhotoUrl = SiteSettings.UploadPath + fileName + "_medium.jpg",
                        };


                        db.Photos.Add(photo);
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, message = ex.Message }, "application/json"));
            }
            return(Json(new { success = true }, "text/html"));
        }
Esempio n. 9
0
 public override bool UnlockUser(string userName)
 {
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == userName);
         if (User != null)
         {
             User.IsLockedOut = false;
             User.PasswordFailuresSinceLastSuccess = 0;
             Context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Esempio n. 10
0
 public override void AddUsersToRoles(string[] usernames, string[] roleNames)
 {
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         List <User> Users = Context.Users.Where(Usr => usernames.Contains(Usr.Username)).ToList();
         List <Role> Roles = Context.Roles.Where(Rl => roleNames.Contains(Rl.RoleName)).ToList();
         foreach (User user in Users)
         {
             foreach (Role role in Roles)
             {
                 if (!user.Roles.Contains(role))
                 {
                     user.Roles.Add(role);
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Esempio n. 11
0
 public override void CreateRole(string roleName)
 {
     if (!string.IsNullOrEmpty(roleName))
     {
         using (ApertureDataContext Context = new ApertureDataContext())
         {
             Role Role = null;
             Role = Context.Roles.FirstOrDefault(Rl => Rl.RoleName == roleName);
             if (Role == null)
             {
                 Role NewRole = new Role
                 {
                     RoleId   = Guid.NewGuid(),
                     RoleName = roleName
                 };
                 Context.Roles.Add(NewRole);
                 Context.SaveChanges();
             }
         }
     }
 }
Esempio n. 12
0
 public override bool DeleteUser(string username, bool deleteAllRelatedData)
 {
     if (string.IsNullOrEmpty(username))
     {
         return(false);
     }
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         User User = null;
         User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
         if (User != null)
         {
             Context.Users.Remove(User);
             Context.SaveChanges();
             return(true);
         }
         else
         {
             return(false);
         }
     }
 }
Esempio n. 13
0
    public static MembershipCreateStatus Register(string Username, string Password, string Email, bool IsApproved, string FirstName, string LastName)
    {
        MembershipCreateStatus CreateStatus;

        Membership.CreateUser(Username, Password, Email, null, null, IsApproved, out CreateStatus);

        if (CreateStatus == MembershipCreateStatus.Success)
        {
            using (ApertureDataContext Context = new ApertureDataContext())
            {
                User User = Context.Users.FirstOrDefault(Usr => Usr.Username == Username);
                User.FirstName = FirstName;
                User.LastName  = LastName;
                Context.SaveChanges();
            }

            if (IsApproved)
            {
                FormsAuthentication.SetAuthCookie(Username, false);
            }
        }

        return(CreateStatus);
    }
Esempio n. 14
0
 public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
 {
     using (ApertureDataContext Context = new ApertureDataContext())
     {
         foreach (String username in usernames)
         {
             String us   = username;
             User   user = Context.Users.FirstOrDefault(U => U.Username == us);
             if (user != null)
             {
                 foreach (String roleName in roleNames)
                 {
                     String rl   = roleName;
                     Role   role = user.Roles.FirstOrDefault(R => R.RoleName == rl);
                     if (role != null)
                     {
                         user.Roles.Remove(role);
                     }
                 }
             }
         }
         Context.SaveChanges();
     }
 }
Esempio n. 15
0
 public void Save()
 {
     context.SaveChanges();
 }
Esempio n. 16
0
    public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
    {
        if (string.IsNullOrEmpty(username))
        {
            status = MembershipCreateStatus.InvalidUserName;
            return(null);
        }
        if (string.IsNullOrEmpty(password))
        {
            status = MembershipCreateStatus.InvalidPassword;
            return(null);
        }
        if (string.IsNullOrEmpty(email))
        {
            status = MembershipCreateStatus.InvalidEmail;
            return(null);
        }

        string HashedPassword = Crypto.HashPassword(password);

        if (HashedPassword.Length > 128)
        {
            status = MembershipCreateStatus.InvalidPassword;
            return(null);
        }

        using (ApertureDataContext Context = new ApertureDataContext())
        {
            if (Context.Users.Where(Usr => Usr.Username == username).Any())
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return(null);
            }

            if (Context.Users.Where(Usr => Usr.Email == email).Any())
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return(null);
            }

            User NewUser = new User
            {
                UserId     = Guid.NewGuid(),
                Username   = username,
                Password   = HashedPassword,
                IsApproved = isApproved,
                Email      = email,
                CreateDate = DateTime.UtcNow,
                LastPasswordChangedDate          = DateTime.UtcNow,
                PasswordFailuresSinceLastSuccess = 0,
                LastLoginDate           = DateTime.UtcNow,
                LastActivityDate        = DateTime.UtcNow,
                LastLockoutDate         = DateTime.UtcNow,
                IsLockedOut             = false,
                LastPasswordFailureDate = DateTime.UtcNow
            };

            Context.Users.Add(NewUser);
            Context.SaveChanges();
            status = MembershipCreateStatus.Success;
            return(new MembershipUser(Membership.Provider.Name, NewUser.Username, NewUser.UserId, NewUser.Email, null, null, NewUser.IsApproved, NewUser.IsLockedOut, NewUser.CreateDate.Value, NewUser.LastLoginDate.Value, NewUser.LastActivityDate.Value, NewUser.LastPasswordChangedDate.Value, NewUser.LastLockoutDate.Value));
        }
    }