Implementing JWT Authentication: .NET Backend with React Frontend

Jagadish Shrestha
Software Engineer
JWT (JSON Web Token) authentication is a popular method for securing web applications. Let's explore how to implement it in a .NET Core backend with a React frontend.
Understanding JWT
JWT is a compact, URL-safe means of representing claims between two parties. It consists of three parts: - Header: Algorithm and token type - Payload: Claims and data - Signature: Verification of the token
Backend Implementation (.NET Core)
- Required Packages
- JWT Configuration
- JWT Service
public class JwtService : IJwtService { private readonly IConfiguration _configuration;
public JwtService(IConfiguration configuration) { _configuration = configuration; }
public string GenerateToken(User user) { var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JwtSettings:SecretKey"])); var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var claims = new[] { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Email, user.Email), new Claim(ClaimTypes.Role, user.Role) };
var token = new JwtSecurityToken( issuer: _configuration["JwtSettings:Issuer"], audience: _configuration["JwtSettings:Audience"], claims: claims, expires: DateTime.Now.AddMinutes(Convert.ToDouble(_configuration["JwtSettings:ExpiryInMinutes"])), signingCredentials: credentials );
return new JwtSecurityTokenHandler().WriteToken(token); } } ```
- Authentication Controller
public AuthController(IJwtService jwtService, IUserService userService) { _jwtService = jwtService; _userService = userService; }
[HttpPost("login")] public async Task<IActionResult> Login(LoginRequest request) { var user = await _userService.ValidateUser(request.Email, request.Password); if (user == null) return Unauthorized();
var token = _jwtService.GenerateToken(user); return Ok(new { token }); } } ```
- Program.cs Configuration
Frontend Implementation (React)
- Authentication Context
interface AuthContextType { token: string | null; login: (token: string) => void; logout: () => void; isAuthenticated: boolean; }
const AuthContext = createContext<AuthContextType | null>(null);
export const AuthProvider: React.FC = ({ children }) => { const [token, setToken] = useState<string | null>(localStorage.getItem('token'));
const login = (newToken: string) => { setToken(newToken); localStorage.setItem('token', newToken); };
const logout = () => { setToken(null); localStorage.removeItem('token'); };
return ( <AuthContext.Provider value={{ token, login, logout, isAuthenticated: !!token }}> {children} </AuthContext.Provider> ); }; ```
- API Service
const api = axios.create({ baseURL: 'https://api.yourbackend.com' });
api.interceptors.request.use((config) => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; });
export const login = async (email: string, password: string) => { const response = await api.post('/api/auth/login', { email, password }); return response.data; }; ```
- Login Component
const Login: React.FC = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const { login: authLogin } = useAuth();
const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); try { const { token } = await login(email, password); authLogin(token); } catch (error) { console.error('Login failed:', error); } };
return ( <form onSubmit={handleSubmit}> <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} placeholder="Email" /> <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} placeholder="Password" /> <button type="submit">Login</button> </form> ); }; ```
- Protected Route Component
const ProtectedRoute: React.FC<{ children: React.ReactNode }> = ({ children }) => { const { isAuthenticated } = useAuth();
if (!isAuthenticated) { return <Navigate to="/login" />; }
return <>{children}</>; }; ```
Security Best Practices
- Token Storage
- Token Validation
- Error Handling
- Additional Security Measures
Common Challenges and Solutions
- Token Expiration
- Cross-Origin Issues
- State Management
Conclusion
Implementing JWT authentication requires careful consideration of security and user experience. Key points to remember: - Secure token storage - Proper error handling - Token refresh mechanism - Security best practices
Remember to: - Keep tokens secure - Implement proper validation - Handle edge cases - Follow security guidelines
JWT authentication provides a robust solution for securing your applications, but it's important to implement it correctly and follow security best practices to ensure your application remains secure.