Created
October 12, 2017 14:57
-
-
Save Swimburger/714673e41ad07999f466f2a3942af061 to your computer and use it in GitHub Desktop.
Change username, password and lockout from console application Asp.net identity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /> | |
</startup> | |
<connectionStrings> | |
<add name="core" connectionString="yourconnectionstring" providerName="System.Data.SqlClient"/> | |
</connectionStrings> | |
<system.web> | |
<membership defaultProvider="sql" hashAlgorithmType="SHA512"> | |
<providers> | |
<clear /> | |
<add name="sql" type="System.Web.Security.SqlMembershipProvider" connectionStringName="core" applicationName="sitecore" minRequiredPasswordLength="1" minRequiredNonalphanumericCharacters="0" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" /> | |
</providers> | |
</membership> | |
</system.web> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
string userName; | |
if (args.Length < 1) | |
{ | |
Console.WriteLine("Please enter a username:"); | |
userName = Console.ReadLine(); | |
} | |
else | |
{ | |
userName = args[0]; | |
} | |
string password; | |
if (args.Length < 2) | |
{ | |
Console.WriteLine("Please enter a password:"); | |
password = Console.ReadLine(); | |
} | |
else | |
{ | |
password = args[1]; | |
} | |
bool unlockUser; | |
if (args.Length < 3) | |
{ | |
Console.WriteLine("Should the user be unlocked? [y,n] (default y)"); | |
var unlockUserResponse = Console.ReadLine(); | |
unlockUser = string.IsNullOrEmpty(unlockUserResponse) || | |
unlockUserResponse.Equals("y", StringComparison.InvariantCultureIgnoreCase) || | |
unlockUserResponse.Equals("yes", StringComparison.InvariantCultureIgnoreCase); | |
} | |
else | |
{ | |
unlockUser = args[2].Equals("true", StringComparison.InvariantCulture); | |
} | |
var user = Membership.GetUser(userName, false); | |
if (user == null) | |
{ | |
Console.WriteLine("User not found"); | |
return; | |
} | |
if (unlockUser) | |
{ | |
var isUnlocked = user.UnlockUser(); | |
Console.WriteLine(isUnlocked ? "User has been unlocked" : "User has not been unlocked"); | |
} | |
var oldPassword = user.ResetPassword(); | |
var passwordHasBeenChanged = user.ChangePassword(oldPassword, password); | |
if (passwordHasBeenChanged) | |
{ | |
Console.WriteLine("Password has been changed"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment