static void DeleteAction(String connString) { String appName = ExtConsole .Create() .LabelWith("App Name: ") .GetString(new SimpleStringValidator("Same as the one from your app.config / web.config")); String hashAlgo = ExtConsole .Create() .LabelWith("Hash Algo: (MD5, SHA1, SHA512) ") .GetString(new SimpleStringValidator("Choose one: MD5, SHA1, SHA512")); String username = ExtConsole .Create() .LabelWith("Username: "******"Input Username you want to delete")); SqlMembershipProvider provider = MembershipService.InitializeAndGetAspMembershipConfig(connString, appName, hashAlgo); provider.DeleteUser(username, true); }
static void ResetAction(String connString) { String appName = ExtConsole .Create() .LabelWith("App Name: ") .GetString(new SimpleStringValidator("Same as the one from your app.config / web.config")); String hashAlgo = ExtConsole .Create() .LabelWith("Hash Algo: (MD5, SHA1, SHA512) ") .GetString(new SimpleStringValidator("Choose one: MD5, SHA1, SHA512")); String username = ExtConsole .Create() .LabelWith("Username: "******"Input Username you want to reset")); String pwd = ExtConsole .Create() .LabelWith("Password: "******"Input new Password")); SqlMembershipProvider provider = MembershipService.InitializeAndGetAspMembershipConfig(connString, appName, hashAlgo); MembershipUser user = provider.GetUser(username, false); if (user == null) { throw new InvalidOperationException("User not found."); } Console.WriteLine($"User '{username}' found."); String reset = provider.ResetPassword(username, null); provider.ChangePassword(username, reset, pwd); UpdateUserLoginProperty(connString, username); }
static void CreateAction(String connString) { String appName = ExtConsole .Create() .LabelWith("App Name: ") .GetString(new SimpleStringValidator("Same as the one from your app.config / web.config")); String hashAlgo = ExtConsole .Create() .LabelWith("Hash Algo: (MD5, SHA1, SHA512) ") .GetString(new SimpleStringValidator("Choose one: MD5, SHA1, SHA512")); String username = ExtConsole .Create() .LabelWith("Username: "******"Input Username you want to create")); String pwd = ExtConsole .Create() .LabelWith("Password: "******"Input new Password")); String email = ExtConsole .Create() .LabelWith("Email: ") .GetString(new SimpleEmailValidator("Email format is invalid")); Console.WriteLine("Roles:"); using (var db = new Database(connString, true)) { db.Query <SimpleResult>(@" BEGIN SET NOCOUNT ON SELECT RoleName Result FROM dbo.aspnet_Roles SET NOCOUNT OFF END") .ToList() .ForEach(result => Console.WriteLine($" > {result.Result}")); } String role = ExtConsole .Create() .LabelWith("Role: ") .GetString(new SimpleStringValidator("Choose one from above")); SqlMembershipProvider provider = MembershipService.InitializeAndGetAspMembershipConfig(connString, appName, hashAlgo); MembershipUser user = provider.CreateUser(username, pwd, email, "Your account might have technical difficulties. Please ask your Administrator.", "TECHNICAL DIFFICULTIES BECAUSE OF RESET", true, Guid.NewGuid(), out MembershipCreateStatus status); IDictionary <MembershipCreateStatus, String> statusMessage = new Dictionary <MembershipCreateStatus, String> { [MembershipCreateStatus.DuplicateUserName] = "Username already exists. Please enter a different user name.", [MembershipCreateStatus.DuplicateEmail] = "A username for that email address already exists. Please enter a different email address.", [MembershipCreateStatus.InvalidPassword] = "The password provided is invalid. Please enter a valid password value.", [MembershipCreateStatus.InvalidEmail] = "The email address provided is invalid. Please check the value and try again.", [MembershipCreateStatus.InvalidAnswer] = "The password retrieval answer provided is invalid. Please check the value and try again.", [MembershipCreateStatus.InvalidQuestion] = "The password retrieval question provided is invalid. Please check the value and try again.", [MembershipCreateStatus.InvalidUserName] = "The user name provided is invalid. Please check the value and try again.", [MembershipCreateStatus.ProviderError] = "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.", [MembershipCreateStatus.UserRejected] = "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.", [MembershipCreateStatus.Success] = "the user creation done in success." }; if (!statusMessage.ContainsKey(status)) { throw new InvalidOperationException("An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."); } Console.WriteLine(statusMessage[status]); using (var db = new Database(connString, true)) { Int32 result = db.NQueryScalar <Int32>(@" BEGIN SET NOCOUNT ON SELECT COUNT(0) FROM dbo.aspnet_Roles WHERE LoweredRoleName = @RoleName SET NOCOUNT OFF END", new { RoleName = role.ToLowerInvariant() }); if (result < 1) { throw new InvalidOperationException($"Role {role} isn't found anywhere."); } } using (var db = new Database(connString, true)) { String result = db.NQueryScalar <String>(@" BEGIN SET NOCOUNT ON BEGIN TRAN AssignRole BEGIN TRY DECLARE @@message VARCHAR(MAX) DECLARE @@roleId UNIQUEIDENTIFIER SELECT TOP 1 @@roleId = RoleId FROM dbo.aspnet_Roles WHERE LoweredRoleName = @RoleName INSERT INTO dbo.aspnet_UsersInRoles (RoleId, UserId) VALUES (@@roleId, @UserId) COMMIT TRAN AssignRole SET @@message = 'S|Finish' END TRY BEGIN CATCH ROLLBACK TRAN AssignRole SET @@message = 'E|' + CAST(ERROR_LINE() AS VARCHAR) + ': ' + ERROR_MESSAGE() END CATCH SET NOCOUNT OFF SELECT @@message [Message] END", new { UserId = user.ProviderUserKey.ToString(), RoleName = role.ToLowerInvariant() }); result.AsActionResponseViewModel(); } UpdateUserLoginProperty(connString, username); }