public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            var result = false;
            if (ValidateUser(username, oldPassword))
            {
                using (var entities = new BAPSLansdaleEntities())
                {
                    var users = from u in entities.Users where (u.UserName == username) select u;
                    if (users.Count() != 0)
                    {
                        var user = users.FirstOrDefault();
                        if (user != null)
                        {
                            user.PasswordSalt = CreateSalt();
                            user.Password = CreatePasswordHash(newPassword, user.PasswordSalt);
                            user.NeedsPasswordChange = false;
                            entities.SaveChanges();
                            result = true;
                        }

                    }
                }
            }
            return result;
        }
 public bool IsUserInRole(string username, string roleName)
 {
     using (var entities = new BAPSLansdaleEntities())
     {
         var users = from u in entities.Users where (u.UserName == username) select u;
         if (users.Count() != 0)
         {
             var user = users.FirstOrDefault();
             var roles = from r in entities.Roles where (r.RoleId == user.RoleID) select r;
             if (roles.Count() != 0)
             {
                 var role = roles.FirstOrDefault();
                 if (role != null && role.Name == roleName) return true;
             }
         }
     }
     return false;
 }
 public string[] GetRolesForUser(string username)
 {
     var result = new List<string>();
     using (var entities = new BAPSLansdaleEntities())
     {
         var users = from u in entities.Users where (u.UserName == username) select u;
         if (users.Count() != 0)
         {
             var user = users.FirstOrDefault();
             var roles = from r in entities.Roles where (r.RoleId == user.RoleID) select r;
             if (roles.Count() != 0)
             {
                 var role = roles.FirstOrDefault();
                 if (role != null) result.Add(role.Name);
             }
         }
     }
     return result.ToArray();
 }
        public MembershipUser CreateUser(string username, string password, string email)
        {
            using (var entities = new BAPSLansdaleEntities())
            {
                var user = new User();
                user.UserName = username;
                user.PasswordSalt = CreateSalt();
                user.Password = CreatePasswordHash(password, user.PasswordSalt);
                user.Email = email;
                user.LastLogin = DateTime.Now;
                user.Active = true;
                user.NeedsPasswordChange = true;
                user.RoleID = GetDefaultRoleId(entities);

                entities.AddToUsers(user);
                entities.SaveChanges();

                return GetUser(username);
            }
        }
        public MembershipUser GetUser(string username)
        {
            using (var entities = new BAPSLansdaleEntities())
            {
                var users = from u in entities.Users where (u.UserName == username) select u;

                if (users.Count() != 0)
                {
                    var dbuser = users.FirstOrDefault();

                    var _username = dbuser.UserName;
                    var _providerUserKey = dbuser.UserId;
                    var _email = dbuser.Email;
                    var _passwordQuestion = string.Empty;
                    var _comment = string.Empty;
                    var _isApproved = true;
                    var _isLockedOut = false;
                    var _creationDate = DateTime.Now;
                    var _lastLoginDate = (DateTime)dbuser.LastLogin;
                    var _lastActivityDate = DateTime.Now;
                    var _lastPasswordChangedDate = DateTime.Now;
                    var _lastLockedOutDate = DateTime.Now;

                    var user = new MembershipUser("CustomMembershipProvider",
                                                              _username,
                                                              _providerUserKey,
                                                              _email,
                                                              _passwordQuestion,
                                                              _comment,
                                                              _isApproved,
                                                              _isLockedOut,
                                                              _creationDate,
                                                              _lastLoginDate,
                                                              _lastActivityDate,
                                                              _lastPasswordChangedDate,
                                                              _lastLockedOutDate);

                    return user;
                }
                else
                {
                    return null;
                }
            }
        }
 private static int GetDefaultRoleId(BAPSLansdaleEntities entities)
 {
     var result = 0;
     foreach (var role in entities.Roles)
     {
         if (role.Name == "DBUser")
         {
             result = role.RoleId;
         }
     }
     return result;
 }
        public bool ValidateUser(string username, string password)
        {
            var result = false;
            using (var entities = new BAPSLansdaleEntities())
            {
                var users = from u in entities.Users where (u.UserName == username) select u;
                if (users.Count() != 0)
                {
                    var user = users.FirstOrDefault();
                    if (user != null)
                    {
                        if (!user.Active)
                            throw new Exception(
                                "User found but it is marked inactive in the database. Please contact your database administrator karyakar.");

                        if (user.Password == CreatePasswordHash(password, user.PasswordSalt) && user.Active)
                        {
                            result = true;
                        }
                    }
                }
            }
            return result;
        }
        public string GetUserNameByEmail(string email)
        {
            using (var entities = new BAPSLansdaleEntities())
            {
                var users = from u in entities.Users where (u.Email == email) select u;

                if (users.Count() != 0)
                {
                    var user = users.FirstOrDefault();
                    return user != null ? user.UserName : string.Empty;
                }
                else
                {
                    return string.Empty;
                }
            }
        }