Authentication is a fundamental requirement in modern web applications built with React on the frontend and ASP.NET Core on the backend. A secure authentication mechanism ensures that only authorized users can access protected APIs and application resources. In enterprise-grade applications, JSON Web Token (JWT) based authentication is commonly used because it is stateless, scalable, and suitable for distributed systems.
This article explains how to implement authentication in a React application with a .NET Core backend using JWT tokens, secure API endpoints, and role-based authorization.
Architecture Overview
In a typical React and ASP.NET Core authentication flow:
The user submits login credentials from the React frontend.
The ASP.NET Core Web API validates credentials.
If valid, the backend generates a JWT token.
The React app securely stores the token.
The token is sent in the Authorization header for protected API requests.
The backend validates the token before granting access.
This stateless authentication approach improves scalability and works well for cloud-native and microservices-based systems.
Step 1: Configure Authentication in ASP.NET Core Backend
Install required NuGet packages:
Microsoft.AspNetCore.Authentication.JwtBearer
Microsoft.IdentityModel.Tokens
Configure JWT authentication in Program.cs:
builder.Services.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
};
});
builder.Services.AddAuthorization();
Add middleware:
app.UseAuthentication();
app.UseAuthorization();
Step 2: Generate JWT Token After Login
Create a login endpoint in your controller:
[HttpPost("login")]
public IActionResult Login(LoginModel model)
{
if (model.Username == "admin" && model.Password == "password")
{
var claims = new[]
{
new Claim(ClaimTypes.Name, model.Username),
new Claim(ClaimTypes.Role, "Admin")
};
var key = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _configuration["Jwt:Issuer"],
audience: _configuration["Jwt:Audience"],
claims: claims,
expires: DateTime.Now.AddMinutes(60),
signingCredentials: creds);
return Ok(new
{
token = new JwtSecurityTokenHandler().WriteToken(token)
});
}
return Unauthorized();
}

Join the conversation! Your thoughts help the community grow.