Categories: .Net Core 3

Identity Server Hashing Passwords For Existing Users

Recently we started working on a new IdentityServer4 project - something to give a new facelift and security to an existing product usin C# and .Net Core 3.

The idea was to create a portal for users to log in through, then forward on to the existing products.

The plus side of this: using the latest .Net Core 3 technologies, adding extra security through the IdentityServer4 and future proof.

The negative: IdentityServer4 used it’s own users table in our Microsoft SQL Server DB.

We already had a number of users that had created account in the old product; so the minimum we wanted to do was to transfer the login details without having to manually recreate them through the Identity Server interface.

The first real issue

The first real issue is the hashing algorithm. The existing user table had hashed passwords and a salt value. We were unable to see what the original password was and so could not reproduce.

To add to that, the salt used for these was generated through a different, PHP based, algorithm. We could not simply copy the passwords and salts into the new table.

This added a second issue; IdentityServer4 had its own password hashing algorithm - so we needed to use this to create the hashed password when we copied/created the users in the copy.

The C# .Net Core3 algorithm I used

After a little digging I round out a little more about the PasswordHasher object. It sounded like the tools for the job. After a little more digging I came up with the following code which I used to hash passwords for my existing users after transferring to the new tables.

IdentityUser _identityUser = new IdentityUser(); 
Guid _guid = new GUID(); String _password;
While Loop() 
{ 
    Read existing table(...) 

    _guid = Guid.NewGuid(); 
    _identityUser.UserName = ReadUserName; 
    _identityUser.Email = ReadEmail; 
    _identityUser.Id = guid.ToString(); 
    _identityUser.NormalisedEmail = Email To Upper;
    var _hasher = new PasswordHasher<IdentityUser>();
    _password = _hasher.HashPassword(_identityUser, @“password”);

    Save to new table(...);
} 

In the above code: you create a new identity user, guid and string for the password. 

Next you create your own method for reading the details from the existing table. Add the appropriate details from this into the identity user structure.

Next you create a object of type PasswordHasher. Finally, pass in the user and password text and you get the hashed password out as a string.

Save all this to the AspNetUsers table (the one used by Identity Server as default) and you’ve will have now created a user that you can use to log into your Identity Server project.

This will allow you to build a small application to bulk copy existing users into the new user login - though they may need to change their password on first login.

As part of this tool you may create a random password generation code then save that out to a log file so that each user has an independent password. 

Check out a future post where I will detail the random password generator code I have used in my project.

Sean

Recent Posts

The Question: Cream or Jam First

So, to set the scene. I'm currently away on a break. I was out walking…

1 year ago

Creating a WordPress SEO Plugin From Scratch

Creating a WordPress SEO plugin from scratch is an ambitious yet rewarding endeavor that can…

1 year ago

A Step-by-Step Guide to Building an API Endpoint with Java Spring Boot

Introduction to API Endpoint Development Understanding API Endpoints API endpoints serve as the communicative bridges…

2 years ago

The Fine Line Between Knowledge and Overwhelm: Signs You're Learning Too Much

The innate human desire to learn and grow is a fundamental part of our nature.…

2 years ago

Demystifying the @Transactional Annotation in Spring Boot

When working within the Spring framework, developers often encounter scenarios where transactional integrity is crucial…

2 years ago

Demystifying @Autowired in Java Spring Boot: A Beginner's Guide

Introduction to @Autowired in Java Spring Boot What is Dependency Injection? Dependency Injection (DI) is…

2 years ago