Skip to content

Instantly share code, notes, and snippets.

@ml-eds
Last active July 26, 2019 14:08
Show Gist options
  • Save ml-eds/1ce877db6bea92ad295f55184f27a81b to your computer and use it in GitHub Desktop.
Save ml-eds/1ce877db6bea92ad295f55184f27a81b to your computer and use it in GitHub Desktop.
Password hashing (ASPNetCore)
using System;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
//PasswordHasher configuration
PasswordHasherOptions opt = new PasswordHasherOptions();
opt.CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV3;
opt.IterationCount = 12;
OptionsWrapper<PasswordHasherOptions> opts = new OptionsWrapper<PasswordHasherOptions>(opt);
//Hash password
PasswordHasher<Object> hash = new PasswordHasher<Object>(opts);
string password = hash.HashPassword(null, "fnord");
Console.WriteLine("Hash of password 'fnord':" + password);
//Verify hash + password
string given = "AQAAAAEAACcQAAAAEH9zn8nECM7n0bgV+ntLs+nJGwnElpjKawrgn+wEhypGmnB/XMZTLsUASGC+6POvrw==";
bool success = PasswordVerificationResult.Success == hash.VerifyHashedPassword(null, given, "fnord");
Console.WriteLine("Verification of " + given + " was succes:" + success );
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment