public SimpleMembershipInitializer() { Database.SetInitializer<UsersContext>(null); try { using (var context = new UsersContext()) { if (!context.Database.Exists()) { // Create the SimpleMembership database without Entity Framework migration schema ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } WebSecurity.InitializeDatabaseConnection( Config.ConnectionStringName, Config.UsersTableName, Config.UsersPrimaryKeyColumnName, Config.UsersUserNameColumnName, autoCreateTables: true); const string tyler = "tyler123"; const string jay = "jay"; const string john = "john123"; const string password = "******"; const string admin = "Administrator"; const string member = "Member"; if (!Roles.RoleExists(admin)) Roles.CreateRole(admin); if(!Roles.RoleExists(member)) Roles.CreateRole(member); if (!WebSecurity.UserExists(jay)) WebSecurity.CreateUserAndAccount(jay, password); if (!WebSecurity.UserExists(john)) WebSecurity.CreateUserAndAccount(john, password); if (!WebSecurity.UserExists(tyler)) WebSecurity.CreateUserAndAccount(tyler, password); if (!Roles.GetRolesForUser(jay).Contains(admin)) Roles.AddUsersToRoles(new[] { jay }, new[] { admin }); if (!Roles.GetRolesForUser(john).Contains(admin)) Roles.AddUsersToRoles(new[] { john }, new[] { admin }); if (!Roles.GetRolesForUser(tyler).Contains(admin)) Roles.AddUsersToRoles(new[] { tyler }, new[] { admin }); } catch (Exception ex) { throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); } }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="userId"></param> /// <param name="rt"></param> /// <returns></returns> public bool GetUserIdAndTokenMatches(UsersContext context, int userId, string rt) { return (from j in context.webpages_Memberships where (j.UserId == userId) && (j.PasswordVerificationToken == rt) select j).Any(); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="userName"></param> /// <returns></returns> public int GetUserId(UsersContext context, string userName) { return (from i in context.UserProfiles where i.DisplayName == userName select i.UserId).FirstOrDefault(); }
public ActionResult ResetPassword(string un, string rt) { var help = new AccountControllerHelpers(UoW, PWS); bool any; using (var db = new UsersContext()) { var userid = help.GetUserId(db, un); //check userid and token matches any = help.GetUserIdAndTokenMatches(db, userid, rt); } if (any) { string newpassword = help.GetNewPasswordPassword(PWS); //reset password bool response = WebSecurity.ResetPassword(rt, newpassword); if (response) help.CreateEmail(un, newpassword); else TempData["Message"] = "Hey, avoid random request on this page."; } else TempData["Message"] = "Username and token not maching."; return View(); }