Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating Have I Been Pwned API in ASP.NET Core for Password Breach Checks

Integrating Have I Been Pwned API in ASP.NET Core for Password Breach Checks

Date- May 25,2026 188
haveibeenpwned aspnetcore

Overview

The Have I Been Pwned API is a powerful tool designed to help developers and organizations determine if a user’s password has been compromised in data breaches. By leveraging this API, applications can proactively warn users to change their passwords, thus improving overall security. This service is vital in an era where data breaches are rampant, and users often reuse passwords across multiple sites.

The API operates on a simple principle: when a password is checked, it is hashed using the SHA-1 algorithm, and only the first five characters of the hash are sent to the API. The API responds with the count of occurrences of that password in known breaches, allowing the application to determine if a password is safe or not. This approach not only maintains user privacy but also minimizes the amount of data sent over the network.

Real-world use cases include applications that require user registration or password changes. By integrating this API, developers can enhance user experience and security by preventing users from selecting compromised passwords. This integration can be particularly useful in applications dealing with sensitive data, such as financial services or healthcare.

Prerequisites

  • ASP.NET Core: Basic understanding of creating and managing an ASP.NET Core application.
  • HTTP Client: Familiarity with making HTTP requests in .NET.
  • NuGet Packages: Knowledge of how to install and manage NuGet packages in your project.
  • JSON Handling: Understanding JSON serialization and deserialization.

Setting Up the ASP.NET Core Project

To begin, you need to set up an ASP.NET Core project. This will serve as the foundation for integrating the Have I Been Pwned API.

dotnet new webapp -n PasswordBreachCheck

This command creates a new ASP.NET Core web application named PasswordBreachCheck. Next, navigate to the project directory:

cd PasswordBreachCheck

Ensure that your project runs correctly by executing:

dotnet run

You should see the default web application running. This project will serve as the basis for integrating our password breach check functionality.

Adding Necessary NuGet Packages

To work with HTTP requests and JSON, you need to add the System.Net.Http and Newtonsoft.Json packages. You can do this using the following commands:

dotnet add package Microsoft.Extensions.Http
dotnet add package Newtonsoft.Json

Creating the Service for API Integration

Next, we will create a service class that handles communication with the Have I Been Pwned API. This class will encapsulate the logic to make HTTP requests and process responses.

using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

public class PasswordBreachService
{
private readonly HttpClient _httpClient;

public PasswordBreachService(HttpClient httpClient)
{
_httpClient = httpClient;
}

public async Task CheckPasswordBreachAsync(string password)
{
var hash = CalculateSha1Hash(password);
var prefix = hash.Substring(0, 5);
var response = await _httpClient.GetAsync($"https://api.pwnedpasswords.com/range/{prefix}");

response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return ParseBreachCount(content, hash.Substring(5).ToUpper());
}

private string CalculateSha1Hash(string password)
{
using (var sha1 = SHA1.Create())
{
var bytes = Encoding.ASCII.GetBytes(password);
var hash = sha1.ComputeHash(bytes);
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}

private int ParseBreachCount(string content, string hashSuffix)
{
foreach (var line in content.Split('\n'))
{
var parts = line.Split(':');
if (parts[0].Equals(hashSuffix, StringComparison.OrdinalIgnoreCase))
{
return int.Parse(parts[1]);
}
}
return 0;
}
}

This service class, PasswordBreachService, is structured to perform the following tasks:

  • Constructor: Accepts an HttpClient instance for making HTTP requests.
  • CheckPasswordBreachAsync: Accepts a password, calculates its SHA-1 hash, and sends a GET request to the Have I Been Pwned API using the first five characters of the hash.
  • CalculateSha1Hash: Converts the password into its SHA-1 hash format.
  • ParseBreachCount: Parses the API response to find the count of breaches for the provided password hash suffix.

Each method is designed to handle specific parts of the API integration, ensuring that the class adheres to the single responsibility principle.

Configuring Dependency Injection

ASP.NET Core uses Dependency Injection (DI) to manage service lifetimes and dependencies. To utilize the PasswordBreachService, you must configure it in the Startup.cs file.

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
services.AddControllersWithViews();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Configuration omitted for brevity...
}
}

This configuration does two things:

  • Registers the PasswordBreachService with the dependency injection container.
  • Sets up an HttpClient specifically for the service, which is managed by the DI container.

Creating the Controller

Now, create a controller that will handle HTTP requests for checking passwords against the breach database. The controller will utilize the PasswordBreachService to perform the checks.

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

public class PasswordController : Controller
{
private readonly PasswordBreachService _passwordBreachService;

public PasswordController(PasswordBreachService passwordBreachService)
{
_passwordBreachService = passwordBreachService;
}

[HttpPost("/checkpassword")]
public async Task CheckPassword(string password)
{
var breachCount = await _passwordBreachService.CheckPasswordBreachAsync(password);
return Json(new { breachCount });
}
}

This PasswordController contains:

  • Dependency Injection: The controller receives an instance of PasswordBreachService through its constructor.
  • CheckPassword Action: A POST action that accepts a password, checks it against the breach database, and returns the breach count as a JSON response.

Creating the Frontend Form

Next, we need a simple frontend form to allow users to input their passwords for breach checks. In the Views directory, create a new view named CheckPassword.cshtml.

@{
ViewData["Title"] = "Check Password Breach";
}

Check Your Password

This HTML code includes:

  • A simple form with an input for the password and a submit button.
  • A jQuery script that handles the form submission, sends the password to the controller, and displays the result.

Testing the Application

Run your application again using the dotnet run command. Navigate to the appropriate route to access the form. Enter a password and submit the form to see the results. If the password has been breached, you will see a message indicating the number of times it has been pwned.

Edge Cases & Gotchas

When integrating with the Have I Been Pwned API, there are several edge cases and gotchas to consider:

  • Network Issues: Ensure proper error handling for network failures or timeouts. If the API is unreachable, inform the user gracefully.
  • Rate Limiting: The API has rate limits. Avoid hammering the API with requests; implement caching strategies for repeated checks.
  • Input Validation: Always validate user input before processing to prevent unnecessary API calls and potential abuse.

Performance & Best Practices

To ensure optimal performance when integrating with the Have I Been Pwned API, consider the following best practices:

  • Asynchronous Patterns: Use asynchronous programming (as demonstrated in the examples) to prevent blocking the main thread and improve responsiveness.
  • Caching: Implement caching for previously checked passwords to reduce the number of API calls, especially for common passwords.
  • Batch Requests: If checking multiple passwords, consider batching requests to minimize latency and improve overall performance.

Real-World Scenario

Let’s consider a realistic scenario where you need to implement password checks during user registration and password change workflows. The following is a mini-project that encapsulates all previous implementations.

using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

public class UserController : Controller
{
private readonly PasswordBreachService _passwordBreachService;

public UserController(PasswordBreachService passwordBreachService)
{
_passwordBreachService = passwordBreachService;
}

[HttpPost("/register")]
public async Task Register(string username, string password)
{
var breachCount = await _passwordBreachService.CheckPasswordBreachAsync(password);
if (breachCount > 0)
{
return BadRequest("Password has been pwned. Please choose a different password.");
}
// Register the user (omitted for brevity)
return Ok("User registered successfully.");
}

[HttpPost("/changepassword")]
public async Task ChangePassword(string oldPassword, string newPassword)
{
var breachCount = await _passwordBreachService.CheckPasswordBreachAsync(newPassword);
if (breachCount > 0)
{
return BadRequest("New password has been pwned. Please choose a different password.");
}
// Change the password (omitted for brevity)
return Ok("Password changed successfully.");
}
}

This UserController demonstrates how to check passwords during user registration and password changes. It provides feedback to users if their chosen passwords are compromised, ensuring a more secure experience.

Conclusion

  • Integrating the Have I Been Pwned API enhances security in applications by preventing the use of compromised passwords.
  • Understanding how to work with HTTP requests and JSON in ASP.NET Core is crucial for API integrations.
  • Implementing best practices, such as caching and handling edge cases, improves the robustness of your implementation.
  • Consider utilizing this API in user registration and password management workflows for a more secure application.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Integrating Currency and Exchange Rate API in ASP.NET Core for Real-Time Forex Data
May 28, 2026
Hangfire Integration in ASP.NET Core: Mastering Background Jobs and Scheduled Tasks
May 11, 2026
Redis Cache Integration in ASP.NET Core - Distributed Caching with StackExchange.Redis
May 09, 2026
Implementing GitHub OAuth Integration in ASP.NET Core for Seamless User Login
Apr 30, 2026
Previous in ASP.NET Core
Integrating Google reCAPTCHA v3 in ASP.NET Core for Seamless Bot …
Next in ASP.NET Core
Integrating Cloudflare Turnstile in ASP.NET Core: A Privacy-First…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    CWE-269: Improper Privilege Management - Implementing the … 336 views
  • 2
    Building Custom Bedrock Add-Ons with JavaScript: A Complet… 1,931 views
  • 3
    Error-An error occurred while processing your request in .… 11,954 views
  • 4
    Elasticsearch Integration in ASP.NET Core - Full-Text Sear… 240 views
  • 5
    Send Email With HTML Template And PDF Using ASP.Net C# 17,177 views
  • 6
    Fix Gemini API Error 429: Quota Exceeded on Free Tier (Sys… 818 views
  • 7
    Integrating Google reCAPTCHA Validation in ASP.NET MVC 6,458 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26677 views
  • Exception Handling Asp.Net Core 21716 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 21170 views
  • How to implement Paypal in Asp.Net Core 20126 views
  • Task Scheduler in Asp.Net core 18197 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1780
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor