在现代Web应用开发中,ASP.NET Core JWT刷新令牌是保障用户会话安全、提升用户体验的重要机制。本文将从零开始,详细讲解如何在ASP.NET Core项目中实现JWT(JSON Web Token)身份验证,并加入刷新令牌功能,让小白开发者也能轻松上手。
JWT通常包含一个较短的有效期(例如15分钟),以减少令牌被盗用的风险。但频繁让用户重新登录体验很差。因此,我们引入刷新令牌(Refresh Token):它是一个长期有效的令牌,用于在访问令牌过期后获取新的访问令牌,而无需用户重新输入账号密码。
确保你已安装:
打开终端,执行以下命令:
dotnet new webapi -n JwtRefreshTokenDemocd JwtRefreshTokenDemo 在项目根目录运行:
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer 在 Program.cs 中添加以下代码:
using Microsoft.IdentityModel.Tokens;using System.Text;var builder = WebApplication.CreateBuilder(args);// 添加JWT认证builder.Services.AddAuthentication(options =>{ options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;}).AddJwtBearer(options =>{ options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = builder.Configuration["Jwt:Issuer"], ValidAudience = builderConfiguration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey( Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]!)) };});builder.Services.AddControllers();var app = builder.Build();app.UseAuthentication();app.UseAuthorization();app.MapControllers();app.Run(); 并在 appsettings.json 中添加配置:
{ "Jwt": { "Key": "ThisIsASecretKeyForDemoOnlyDoNotUseInProduction", "Issuer": "https://localhost:5001", "Audience": "https://localhost:5001" }} 首先定义用户和令牌响应模型:
public class User{ public string Username { get; set; } = string.Empty; public string Password { get; set; } = string.Empty;}public class AuthResponse{ public string AccessToken { get; set; } = string.Empty; public string RefreshToken { get; set; } = string.Empty; public DateTime AccessTokenExpiry { get; set; } public DateTime RefreshTokenExpiry { get; set; }} 创建一个服务类 TokenService.cs:
using Microsoft.IdentityModel.Tokens;using System.IdentityModel.Tokens.Jwt;using System.Security.Claims;using System.Text;public class TokenService{ private readonly IConfiguration _config; public TokenService(IConfiguration config) { _config = config; } public (string accessToken, DateTime expiry) GenerateAccessToken(string username) { var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.UTF8.GetBytes(_config["Jwt:Key"]!); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, username) }), Expires = DateTime.UtcNow.AddMinutes(15), // 短期有效 Issuer = _config["Jwt:Issuer"], Audience = _config["Jwt:Audience"], SigningCredentials = new SigningCredentials( new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature) }; var token = tokenHandler.CreateToken(tokenDescriptor); return (tokenHandler.WriteToken(token), token.ValidTo); } public (string refreshToken, DateTime expiry) GenerateRefreshToken() { var refreshToken = Guid.NewGuid().ToString(); var expiry = DateTime.UtcNow.AddDays(7); // 刷新令牌有效期7天 return (refreshToken, expiry); }} 在 Program.cs 中注册服务:
builder.Services.AddSingleton(); 新建 AuthController.cs:
[ApiController][Route("[controller]")]public class AuthController : ControllerBase{ private readonly TokenService _tokenService; public AuthController(TokenService tokenService) { _tokenService = tokenService; } [HttpPost("login")] public IActionResult Login([FromBody] User user) { // 简化:实际应验证数据库中的用户名密码 if (user.Username == "admin" && user.Password == "123456") { var (accessToken, accessExpiry) = _tokenService.GenerateAccessToken(user.Username); var (refreshToken, refreshExpiry) = _tokenService.GenerateRefreshToken(); // 实际项目中应将刷新令牌存入数据库并关联用户 return Ok(new AuthResponse { AccessToken = accessToken, RefreshToken = refreshToken, AccessTokenExpiry = accessExpiry, RefreshTokenExpiry = refreshExpiry }); } return Unauthorized(); } [HttpPost("refresh")] public IActionResult Refresh([FromBody] RefreshRequest request) { // 此处应验证refreshToken是否存在于数据库且未过期 // 为简化,此处跳过验证 var (accessToken, accessExpiry) = _tokenService.GenerateAccessToken("admin"); var (newRefreshToken, newRefreshExpiry) = _tokenService.GenerateRefreshToken(); return Ok(new AuthResponse { AccessToken = accessToken, RefreshToken = newRefreshToken, AccessTokenExpiry = accessExpiry, RefreshTokenExpiry = newRefreshExpiry }); }}public class RefreshRequest{ public string RefreshToken { get; set; } = string.Empty;} 在生产环境中,务必:
通过本教程,你已掌握如何在ASP.NET Core JWT刷新令牌系统中实现安全的身份验证流程。这种模式广泛应用于现代API开发中,是.NET Core安全认证的最佳实践之一。记住,刷新令牌实现不仅要功能完整,更要注重安全性。
希望这篇关于JWT身份验证的教程对你有所帮助!如有疑问,欢迎留言交流。
本文由主机测评网于2025-12-29发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/20251213792.html