Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change username, password and lockout from console application Asp.net identity
<?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>
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
You can’t perform that action at this time.