Introduction
When building secure applications in ASP.NET Core or any .NET-based system, handling user passwords correctly is one of the most critical responsibilities. Storing plain-text passwords is a serious security risk and can lead to data breaches if your database is compromised.
To protect user credentials, developers use password hashing techniques. One of the most trusted and widely used approaches is bcrypt password hashing in .NET.
Bcrypt is designed specifically for securely hashing passwords, making it resistant to brute-force attacks and modern hardware-based cracking attempts.
In this guide, we will walk through how to implement secure password hashing using bcrypt in .NET step by step, along with practical examples and best practices used in real-world applications.
What is Password Hashing?
Password hashing is the process of converting a plain-text password into a fixed-length string using a cryptographic algorithm.
Key characteristics:
One-way transformation (cannot be reversed)
Same input produces same hash (with same salt)
Used to verify passwords without storing them
Example
Password:
mypassword123
Hashed Output:
$2a$11$e0NRo4l7... (truncated)
Instead of storing the original password, only the hash is stored in the database.
Why Use Bcrypt for Password Hashing in .NET?
Bcrypt is specifically designed for password security.
Key Benefits
Built-in salting mechanism
Adjustable work factor (cost)
Resistant to brute-force attacks
Widely adopted in production systems
Unlike simple hashing algorithms like SHA256, bcrypt is intentionally slow, which makes it more secure.
Step 1: Install Bcrypt Package in .NET
To use bcrypt in ASP.NET Core, install the library:
dotnet add package BCrypt.Net-Next
Explanation
BCrypt.Net-Nextis a popular library for bcrypt hashing in .NETIt provides simple APIs for hashing and verifying passwords
Step 2: Hash a Password Before Storing
using BCrypt.Net;
string password = "MySecurePassword123";
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);
Console.WriteLine(hashedPassword);
Explanation
HashPassword()automatically generates a saltThe result includes salt + hash combined
This hash is what you store in your database
Step 3: Store Hashed Password in Database
Example User Model:
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
}
Explanation
Never store plain passwords
Only store
PasswordHashEven if database is leaked, original password is protected
Step 4: Verify Password During Login
string enteredPassword = "MySecurePassword123";
bool isValid = BCrypt.Net.BCrypt.Verify(enteredPassword, hashedPassword);
if (isValid)
{
Console.WriteLine("Login successful");
}
else
{
Console.WriteLine("Invalid credentials");
}

Join the conversation! Your thoughts help the community grow.