예제 #1
0
        public int CreateStockwinnersMember(string emailAddress, string password, string firstName, string lastName, out MembershipCreateStatus status)
        {
            status = MembershipCreateStatus.Success;
            DatabaseContext db = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(DatabaseContext)) as DatabaseContext;

            // Is member unique?
            if (db.StockwinnersMembers.FirstOrDefault(m => m.EmailAddress == emailAddress) != null)
            {
                status = MembershipCreateStatus.DuplicateEmail;

                return(0);
            }

            StockwinnersMember newMember = new StockwinnersMember()
            {
                EmailAddress   = emailAddress,
                FirstName      = firstName,
                LastName       = lastName,
                Password       = MembershipProvider.HashPassword(password),
                IsLegacyMember = false
            };

            db.StockwinnersMembers.Add(newMember);

            db.SaveChanges();

            return(newMember.MemberId);
        }
예제 #2
0
        public ActionResult ChangeEmail(ChangeEmail model)
        {
            if (ModelState.IsValid)
            {
                StockwinnersMember member = _db.StockwinnersMembers.Where(m => m.EmailAddress == model.EmailAddressCurrent).FirstOrDefault();

                if (member == null)
                {
                    ViewBag.Message = "No stockwinners member with the email address exists. Either the email address provided is wrong, or the user has signed up via Facebook or Google in which case we can't change their email address";
                }
                else
                {
                    User user = _db.Users.Where(u => u.EmailAddress == model.EmailAddressCurrent).FirstOrDefault();

                    if (user == null)
                    {
                        ViewBag.Message = "No user with the provided email address exists.";
                    }
                    else
                    {
                        member.EmailAddress = model.EmailAddressNew;
                        user.EmailAddress   = model.EmailAddressNew;

                        ViewBag.Message = "The email has successfully been changed. Please instruct the user to use the new email address to login henceforth.";

                        _db.SaveChanges();
                    }
                }
            }

            return(this.View(model));
        }
예제 #3
0
        public override bool ValidateUser(string username, string password)
        {
            DatabaseContext    db     = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(DatabaseContext)) as DatabaseContext;
            StockwinnersMember member = db.StockwinnersMembers.FirstOrDefault(m => m.EmailAddress == username);

            return(member != null && MembershipProvider.VerifyPassword(password, member.Password, member.IsLegacyMember));
        }
예제 #4
0
        /// <summary>
        /// Authenticates a Stockwinners member. Returns false if the provided login credentials are invalid.
        /// </summary>
        public static bool AuthenticateOrRedirectStockwinnersMember(string emailAddress, string password, string redirectUrl, bool rememberUser)
        {
            WebSite.Infrastructure.MembershipProvider memberProvider = Membership.Provider as WebSite.Infrastructure.MembershipProvider;

            // Lookup the Stockwinners member to locate first and last names
            StockwinnersMember member = memberProvider.GetStockwinnersMember(emailAddress, password);

            if (member != null)
            {
                Authentication.SetCurrentUser(new LoggedInUserIdentity()
                {
                    FirstName                = member.FirstName,
                    LastName                 = member.LastName,
                    EmailAddress             = member.EmailAddress,
                    IdentityProvider         = IdentityProvider.Stockwinners,
                    IdentityProviderIssuedId = member.MemberId.ToString()
                }, rememberUser);

                Authentication.Redirect(redirectUrl);

                return(true);
            }

            return(false);
        }
예제 #5
0
        public StockwinnersMember GetStockwinnersMember(string emailAddress, string password)
        {
            DatabaseContext    db     = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(DatabaseContext)) as DatabaseContext;
            StockwinnersMember member = db.StockwinnersMembers.FirstOrDefault(m => m.EmailAddress == emailAddress);

            if (member != null && MembershipProvider.VerifyPassword(password, member.Password, member.IsLegacyMember))
            {
                return(member);
            }

            return(null);
        }
예제 #6
0
        public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            MembershipUserCollection results = new MembershipUserCollection();

            totalRecords = 0;
            DatabaseContext    db     = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(DatabaseContext)) as DatabaseContext;
            StockwinnersMember member = db.StockwinnersMembers.FirstOrDefault(m => m.EmailAddress == emailToMatch);

            if (member != null)
            {
                results.Add(MembershipProvider.GetMembershipUser(member));
                totalRecords = 1;
            }

            return(results);
        }
예제 #7
0
        public override string ResetPassword(string username, string answer)
        {
            DatabaseContext    db     = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(DatabaseContext)) as DatabaseContext;
            StockwinnersMember member = db.StockwinnersMembers.FirstOrDefault(m => m.EmailAddress == username);

            if (member != null)
            {
                string newPassword = MembershipProvider.RandomString(9);
                member.IsLegacyMember = false;
                member.Password       = MembershipProvider.HashPassword(newPassword);

                db.SaveChanges();

                return(newPassword);
            }

            return(null);
        }
예제 #8
0
        public bool ChangePassword(string username, string newPassword)
        {
            DatabaseContext    db     = System.Web.Mvc.DependencyResolver.Current.GetService(typeof(DatabaseContext)) as DatabaseContext;
            StockwinnersMember member = db.StockwinnersMembers.FirstOrDefault(u => u.EmailAddress == username);

            if (member != null)
            {
                member.Password = MembershipProvider.HashPassword(newPassword);

                // Update the legacy member's bit so that the next time the new hash algorithm is used to verify the user's password
                member.IsLegacyMember = false;

                db.SaveChanges();

                return(true);
            }

            return(false);
        }
예제 #9
0
        private static void SeedAdministrators(DatabaseContext context)
        {
            StockwinnersMember ameen = new StockwinnersMember()
            {
                EmailAddress = "*****@*****.**", FirstName = "Ameen", LastName = "Tayyebi", IsLegacyMember = false, Password = WebSite.Infrastructure.MembershipProvider.HashPassword("madmoney")
            };
            StockwinnersMember mehdi = new StockwinnersMember()
            {
                EmailAddress = "*****@*****.**", FirstName = "Mehdi", LastName = "Ghaffari", IsLegacyMember = false, Password = WebSite.Infrastructure.MembershipProvider.HashPassword("madmoney")
            };
            StockwinnersMember dayee = new StockwinnersMember()
            {
                EmailAddress = "*****@*****.**", FirstName = "Mohammad", LastName = "Mohammadi", IsLegacyMember = false, Password = WebSite.Infrastructure.MembershipProvider.HashPassword("madmoney")
            };

            //context.StockwinnersMembers.AddOrUpdate(member => member.EmailAddress, ameen);
            //context.StockwinnersMembers.AddOrUpdate(member => member.EmailAddress, mehdi);
            context.StockwinnersMembers.AddOrUpdate(member => member.EmailAddress, dayee);

            // Make any user with [email protected] or [email protected] or [email protected] an admin
            string[] adminEmails = new string[] { "*****@*****.**", "*****@*****.**", "*****@*****.**" };
            Role     adminRole   = context.Roles.First(role => role.Name == PredefinedRoles.Administrator);

            foreach (string adminEmail in adminEmails)
            {
                foreach (User admin in context.Users.Include("Roles").Where(user => user.EmailAddress == adminEmail))
                {
                    if (admin != null)
                    {
                        ICollection <Role> roles = admin.Roles ?? new List <Role>();

                        if (!roles.Any(role => role.RoleId == adminRole.RoleId))
                        {
                            roles.Add(adminRole);
                        }
                    }
                }
            }
        }
예제 #10
0
 private static MembershipUser GetMembershipUser(StockwinnersMember member)
 {
     // We don't supply any real dates as those are stored in the real User data model
     return(new MembershipUser("DefaultMembershipProvider", member.EmailAddress, null, member.EmailAddress, null,
                               null, true, false, DateTime.UtcNow, DateTime.UtcNow, DateTime.UtcNow, DateTime.UtcNow, DateTime.UtcNow));
 }