.NET 6實(shí)現(xiàn)基于JWT的Identity功能方法詳解
需求
在.NET Web API開(kāi)發(fā)中還有一個(gè)很重要的需求是關(guān)于身份認(rèn)證和授權(quán)的,這個(gè)主題非常大,所以本文不打算面面俱到地介紹整個(gè)主題,而僅使用.NET框架自帶的認(rèn)證和授權(quán)中間件去實(shí)現(xiàn)基于JWT的身份認(rèn)證和授權(quán)功能。一些關(guān)于這個(gè)主題的基本概念也不會(huì)花很多的篇幅去講解,我們還是專(zhuān)注在實(shí)現(xiàn)上。
目標(biāo)
為TodoList項(xiàng)目增加身份認(rèn)證和授權(quán)功能。
原理與思路
為了實(shí)現(xiàn)身份認(rèn)證和授權(quán)功能,我們需要使用.NET自帶的Authentication和Authorization組件。在本文中我們不會(huì)涉及Identity Server的相關(guān)內(nèi)容,這是另一個(gè)比較大的主題,因?yàn)樵S可證的變更,Identity Server 4將不再能夠免費(fèi)應(yīng)用于盈利超過(guò)一定限額的商業(yè)應(yīng)用中,詳情見(jiàn)官網(wǎng)IdentityServer。微軟同時(shí)也在將廣泛使用的IdentityServer的相關(guān)功能逐步集成到框架中:ASP.NET Core 6 and Authentication Servers,在本文中同樣暫不會(huì)涉及。
實(shí)現(xiàn)
引入Identity組件
我們?cè)?code>Infrastructure項(xiàng)目中添加以下Nuget包:
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" /> <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.1" />
并新建Identity目錄用于存放有關(guān)認(rèn)證和授權(quán)的具體功能,首先添加用戶(hù)類(lèi)ApplicationUser:
ApplicationUser.cs
using Microsoft.AspNetCore.Identity;
namespace TodoList.Infrastructure.Identity;
public class ApplicationUser : IdentityUser
{
// 不做定制化實(shí)現(xiàn),僅使用原生功能
}
由于我們希望使用現(xiàn)有的SQL Server數(shù)據(jù)庫(kù)來(lái)存儲(chǔ)認(rèn)證相關(guān)的信息,所以還需要修改DbContext:
TodoListDbContext.cs
public class TodoListDbContext : IdentityDbContext<ApplicationUser>
{
private readonly IDomainEventService _domainEventService;
public TodoListDbContext(
DbContextOptions<TodoListDbContext> options,
IDomainEventService domainEventService) : base(options)
{
_domainEventService = domainEventService;
}
// 省略其他...
}
為了后面演示的方便,我們還可以在添加種子數(shù)據(jù)的邏輯里增加內(nèi)置用戶(hù)數(shù)據(jù):
TodoListDbContextSeed.cs
// 省略其他...
public static async Task SeedDefaultUserAsync(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
var administratorRole = new IdentityRole("Administrator");
if (roleManager.Roles.All(r => r.Name != administratorRole.Name))
{
await roleManager.CreateAsync(administratorRole);
}
var administrator = new ApplicationUser { UserName = "admin@localhost", Email = "admin@localhost" };
if (userManager.Users.All(u => u.UserName != administrator.UserName))
{
// 創(chuàng)建的用戶(hù)名為admin@localhost,密碼是admin123,角色是Administrator
await userManager.CreateAsync(administrator, "admin123");
await userManager.AddToRolesAsync(administrator, new[] { administratorRole.Name });
}
}
并在ApplicationStartupExtensions中修改:
ApplicationStartupExtensions.cs
public static class ApplicationStartupExtensions
{
public static async Task MigrateDatabase(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<TodoListDbContext>();
context.Database.Migrate();
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>();
// 生成內(nèi)置用戶(hù)
await TodoListDbContextSeed.SeedDefaultUserAsync(userManager, roleManager);
// 省略其他...
}
catch (Exception ex)
{
throw new Exception($"An error occurred migrating the DB: {ex.Message}");
}
}
}
最后我們需要來(lái)修改DependencyInjection部分,以引入身份認(rèn)證和授權(quán)服務(wù):
DependencyInjection.cs
// 省略其他....
// 配置認(rèn)證服務(wù)
// 配置認(rèn)證服務(wù)
services
.AddDefaultIdentity<ApplicationUser>(o =>
{
o.Password.RequireDigit = true;
o.Password.RequiredLength = 6;
o.Password.RequireLowercase = true;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.User.RequireUniqueEmail = true;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<TodoListDbContext>()
.AddDefaultTokenProviders();
添加認(rèn)證服務(wù)
在Applicaiton/Common/Interfaces中添加認(rèn)證服務(wù)接口IIdentityService:
IIdentityService.cs
namespace TodoList.Application.Common.Interfaces;
public interface IIdentityService
{
// 出于演示的目的,只定義以下方法,實(shí)際使用的認(rèn)證服務(wù)會(huì)提供更多的方法
Task<string> CreateUserAsync(string userName, string password);
Task<bool> ValidateUserAsync(UserForAuthentication userForAuthentication);
Task<string> CreateTokenAsync();
}
然后在Infrastructure/Identity中實(shí)現(xiàn)IIdentityService接口:
IdentityService.cs
namespace TodoList.Infrastructure.Identity;
public class IdentityService : IIdentityService
{
private readonly ILogger<IdentityService> _logger;
private readonly IConfiguration _configuration;
private readonly UserManager<ApplicationUser> _userManager;
private ApplicationUser? User;
public IdentityService(
ILogger<IdentityService> logger,
IConfiguration configuration,
UserManager<ApplicationUser> userManager)
{
_logger = logger;
_configuration = configuration;
_userManager = userManager;
}
public async Task<string> CreateUserAsync(string userName, string password)
{
var user = new ApplicationUser
{
UserName = userName,
Email = userName
};
await _userManager.CreateAsync(user, password);
return user.Id;
}
public async Task<bool> ValidateUserAsync(UserForAuthentication userForAuthentication)
{
User = await _userManager.FindByNameAsync(userForAuthentication.UserName);
var result = User != null && await _userManager.CheckPasswordAsync(User, userForAuthentication.Password);
if (!result)
{
_logger.LogWarning($"{nameof(ValidateUserAsync)}: Authentication failed. Wrong username or password.");
}
return result;
}
public async Task<string> CreateTokenAsync()
{
// 暫時(shí)還不來(lái)實(shí)現(xiàn)這個(gè)方法
throw new NotImplementedException();
}
}
并在DependencyInjection中進(jìn)行依賴(lài)注入:
DependencyInjection.cs
// 省略其他... // 注入認(rèn)證服務(wù) services.AddTransient<IIdentityService, IdentityService>();
現(xiàn)在我們來(lái)回顧一下已經(jīng)完成的部分:我們配置了應(yīng)用程序使用內(nèi)建的Identity服務(wù)并使其使用已有的數(shù)據(jù)庫(kù)存儲(chǔ);我們生成了種子用戶(hù)數(shù)據(jù);還實(shí)現(xiàn)了認(rèn)證服務(wù)的功能。
在繼續(xù)下一步之前,我們需要對(duì)數(shù)據(jù)庫(kù)做一次Migration,使認(rèn)證鑒權(quán)相關(guān)的數(shù)據(jù)表生效:

$ dotnet ef database update -p src/TodoList.Infrastructure/TodoList.Infrastructure.csproj -s src/TodoList.Api/TodoList.Api.csproj
Build started...
Build succeeded.
[14:04:02 INF] Entity Framework Core 6.0.1 initialized 'TodoListDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer:6.0.1' with options: MigrationsAssembly=TodoList.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
# 創(chuàng)建相關(guān)數(shù)據(jù)表...
[14:04:03 INF] Executed DbCommand (43ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE [AspNetRoles] (
[Id] nvarchar(450) NOT NULL,
[Name] nvarchar(256) NULL,
[NormalizedName] nvarchar(256) NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);
# 省略中間的部分..
[14:04:03 INF] Executed DbCommand (18ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20220108060343_AddIdentities', N'6.0.1');
Done.
運(yùn)行Api程序,然后去數(shù)據(jù)庫(kù)確認(rèn)一下生成的數(shù)據(jù)表:

種子用戶(hù):

以及角色:

到目前為止,我已經(jīng)集成了Identity框架,接下來(lái)我們開(kāi)始實(shí)現(xiàn)基于JWT的認(rèn)證和API的授權(quán)功能:
使用JWT認(rèn)證和定義授權(quán)方式
在Infrastructure項(xiàng)目的DependencyInjection中添加JWT認(rèn)證配置:
DependencyInjection.cs
// 省略其他...
// 添加認(rèn)證方法為JWT Token認(rèn)證
services
.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = configuration.GetSection("JwtSettings")["validIssuer"],
ValidAudience = configuration.GetSection("JwtSettings")["validAudience"],
// 出于演示的目的,我將SECRET值在這里fallback成了硬編碼的字符串,實(shí)際環(huán)境中,最好是需要從環(huán)境變量中進(jìn)行獲取,而不應(yīng)該寫(xiě)在代碼中
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("SECRET") ?? "TodoListApiSecretKey"))
};
});
// 添加授權(quán)Policy是基于角色的,策略名稱(chēng)為OnlyAdmin,策略要求具有Administrator角色
services.AddAuthorization(options =>
options.AddPolicy("OnlyAdmin", policy => policy.RequireRole("Administrator")));
引入認(rèn)證授權(quán)中間件
在Api項(xiàng)目的Program中,MapControllers上面引入:
Program.cs
// 省略其他... app.UseAuthentication(); app.UseAuthorization();
添加JWT配置
appsettings.Development.json
"JwtSettings": {
"validIssuer": "TodoListApi",
"validAudience": "http://localhost:5050",
"expires": 5
}
增加認(rèn)證用戶(hù)Model
在Application/Common/Models中添加用于用戶(hù)認(rèn)證的類(lèi)型:
UserForAuthentication.cs
using System.ComponentModel.DataAnnotations;
namespace TodoList.Application.Common.Models;
public record UserForAuthentication
{
[Required(ErrorMessage = "username is required")]
public string? UserName { get; set; }
[Required(ErrorMessage = "password is required")]
public string? Password { get; set; }
}
實(shí)現(xiàn)認(rèn)證服務(wù)CreateToken方法
因?yàn)楸酒恼挛覀儧](méi)有使用集成的IdentityServer組件,而是應(yīng)用程序自己去發(fā)放Token,那就需要我們?nèi)?shí)現(xiàn)CreateTokenAsync方法:
IdentityService.cs
// 省略其他...
public async Task<string> CreateTokenAsync()
{
var signingCredentials = GetSigningCredentials();
var claims = await GetClaims();
var tokenOptions = GenerateTokenOptions(signingCredentials, claims);
return new JwtSecurityTokenHandler().WriteToken(tokenOptions);
}
private SigningCredentials GetSigningCredentials()
{
// 出于演示的目的,我將SECRET值在這里fallback成了硬編碼的字符串,實(shí)際環(huán)境中,最好是需要從環(huán)境變量中進(jìn)行獲取,而不應(yīng)該寫(xiě)在代碼中
var key = Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("SECRET") ?? "TodoListApiSecretKey");
var secret = new SymmetricSecurityKey(key);
return new SigningCredentials(secret, SecurityAlgorithms.HmacSha256);
}
private async Task<List<Claim>> GetClaims()
{
// 演示了返回用戶(hù)名和Role兩類(lèi)Claims
var claims = new List<Claim>
{
new(ClaimTypes.Name, User!.UserName)
};
var roles = await _userManager.GetRolesAsync(User);
claims.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role)));
return claims;
}
private JwtSecurityToken GenerateTokenOptions(SigningCredentials signingCredentials, List<Claim> claims)
{
// 配置JWT選項(xiàng)
var jwtSettings = _configuration.GetSection("JwtSettings");
var tokenOptions = new JwtSecurityToken
(
jwtSettings["validIssuer"],
jwtSettings["validAudience"],
claims,
expires: DateTime.Now.AddMinutes(Convert.ToDouble(jwtSettings["expires"])),
signingCredentials: signingCredentials
);
return tokenOptions;
}
添加認(rèn)證接口
在Api項(xiàng)目中新建一個(gè)Controller用于實(shí)現(xiàn)獲取Token的接口:
AuthenticationController.cs
using Microsoft.AspNetCore.Mvc;
using TodoList.Application.Common.Interfaces;
using TodoList.Application.Common.Models;
namespace TodoList.Api.Controllers;
[ApiController]
public class AuthenticationController : ControllerBase
{
private readonly IIdentityService _identityService;
private readonly ILogger<AuthenticationController> _logger;
public AuthenticationController(IIdentityService identityService, ILogger<AuthenticationController> logger)
{
_identityService = identityService;
_logger = logger;
}
[HttpPost("login")]
public async Task<IActionResult> Authenticate([FromBody] UserForAuthentication userForAuthentication)
{
if (!await _identityService.ValidateUserAsync(userForAuthentication))
{
return Unauthorized();
}
return Ok(new { Token = await _identityService.CreateTokenAsync() });
}
}
保護(hù)API資源
我們準(zhǔn)備使用創(chuàng)建TodoList接口來(lái)演示認(rèn)證和授權(quán)功能,所以添加屬性如下:
// 省略其他...
[HttpPost]
// 演示使用Policy的授權(quán)
[Authorize(Policy = "OnlyAdmin")]
[ServiceFilter(typeof(LogFilterAttribute))]
public async Task<ApiResponse<Domain.Entities.TodoList>> Create([FromBody] CreateTodoListCommand command)
{
return ApiResponse<Domain.Entities.TodoList>.Success(await _mediator.Send(command));
}
驗(yàn)證
驗(yàn)證1: 驗(yàn)證直接訪(fǎng)問(wèn)創(chuàng)建TodoList接口
啟動(dòng)Api項(xiàng)目,直接執(zhí)行創(chuàng)建TodoList的請(qǐng)求:

得到了401 Unauthorized結(jié)果。
驗(yàn)證2: 獲取Token
請(qǐng)求獲取Token的接口:

可以看到我們已經(jīng)拿到了JWT Token,把這個(gè)Token放到JWT解析一下可以看到:

主要在payload中可以看到兩個(gè)Claims和其他配置的信息。
驗(yàn)證3: 攜帶Token訪(fǎng)問(wèn)創(chuàng)建TodoList接口
選擇Bearer Token驗(yàn)證方式并填入獲取到的Token,再次請(qǐng)求創(chuàng)建TodoList:

驗(yàn)證4: 更換Policy
修改Infrastructure/DependencyInjection.cs
// 省略其他...
// 添加授權(quán)Policy是基于角色的,策略名稱(chēng)為OnlyAdmin,策略要求具有Administrator角色
services.AddAuthorization(options =>
{
options.AddPolicy("OnlyAdmin", policy => policy.RequireRole("Administrator"));
options.AddPolicy("OnlySuper", policy => policy.RequireRole("SuperAdmin"));
});
并修改創(chuàng)建TodoList接口的授權(quán)Policy:
// 省略其他... [Authorize(Policy = "OnlySuper")]
還是使用admin@locahost用戶(hù)的用戶(hù)名和密碼獲取最新的Token后,攜帶Token請(qǐng)求創(chuàng)建新的TodoList:

得到了403 Forbidden返回,并且從日志中我們可以看到:

告訴我們需要一個(gè)具有SuperAdmin角色的用戶(hù)的合法Token才會(huì)被授權(quán)。
那么到此為止,我們已經(jīng)實(shí)現(xiàn)了基于.NET自帶的Identity框架,發(fā)放Token,完成認(rèn)證和授權(quán)的功能。
一點(diǎn)擴(kuò)展
關(guān)于在.NET Web API項(xiàng)目中進(jìn)行認(rèn)證和授權(quán)的主題非常龐大,首先是認(rèn)證的方式可以有很多種,除了我們?cè)诒疚闹醒菔镜幕贘WT Token的認(rèn)證方式以外,還有OpenId認(rèn)證,基于Azure Active Directory的認(rèn)證,基于OAuth協(xié)議的認(rèn)證等等;其次是關(guān)于授權(quán)的方式也有很多種,可以是基于角色的授權(quán),可以是基于Claims的授權(quán),可以是基于Policy的授權(quán),也可以自定義更多的授權(quán)方式。然后是具體的授權(quán)服務(wù)器的實(shí)現(xiàn),有基于Identity Server 4的實(shí)現(xiàn),當(dāng)然在其更改過(guò)協(xié)議后,我們可以轉(zhuǎn)而使用.NET中移植進(jìn)來(lái)的IdentityServer組件實(shí)現(xiàn),配置的方式也有很多。
由于IdentityServer涉及的知識(shí)點(diǎn)過(guò)于龐雜,所以本文并沒(méi)有試圖全部講到,考慮后面單獨(dú)出一個(gè)系列來(lái)講關(guān)于IdentityServer在.NET 6 Web API開(kāi)發(fā)中的應(yīng)用。
總結(jié)
在本文中,我們實(shí)現(xiàn)了基于JWT Token的認(rèn)證和授權(quán)。下一篇文章我們來(lái)看看為什么需要以及如何實(shí)現(xiàn)Refresh Token機(jī)制。
參考資料
ASP.NET Core 6 and Authentication Servers
以上就是.NET 6實(shí)現(xiàn)基于JWT的Identity功能方法詳解的詳細(xì)內(nèi)容,更多關(guān)于.NET 6基于JWT的Identity功能的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在ASP.NET中讀寫(xiě)XML數(shù)據(jù)的多種方法
在ASP.NET日常開(kāi)發(fā)中,XML(可擴(kuò)展標(biāo)記語(yǔ)言)是一種常用的數(shù)據(jù)交換格式,它被廣泛用于配置文件、數(shù)據(jù)傳輸和Web服務(wù)等場(chǎng)景,在.NET框架中,提供了多種類(lèi)和方法來(lái)讀寫(xiě)XML數(shù)據(jù),以下是對(duì)ASP.NET中讀寫(xiě)XML的詳解,需要的朋友可以參考下2025-01-01
未處理的事件"PageIndexChanging" 之解決方案
今天我寫(xiě)一個(gè)小程序遇到這個(gè)問(wèn)題,上網(wǎng)搜了一下,已經(jīng)有很好的解決方法了,以前都是拉控件自己生成,現(xiàn)在用代碼自己寫(xiě)就出現(xiàn)了這個(gè)問(wèn)題2008-07-07
sql server中批量插入與更新兩種解決方案分享(asp.net)
xml和表值函數(shù)的相對(duì)復(fù)雜些這里簡(jiǎn)單貼一下bcp和SqlDataAdapter進(jìn)行批量跟新插入方法,未經(jīng)整理還望見(jiàn)諒2012-05-05
asp.net GridView模板列中實(shí)現(xiàn)選擇行功能
近來(lái)在項(xiàng)目中用到了GridView控件,用它實(shí)現(xiàn)添加、修改、刪除、選擇、顯示復(fù)雜表頭等功能2010-07-07
解決asp.net Sharepoint無(wú)法連接發(fā)布自定義字符串處理程序,不能進(jìn)行輸出緩存處理的方法
解決Sharepoint無(wú)法連接發(fā)布自定義字符串處理程序,不能進(jìn)行輸出緩存處理的方法2010-03-03
Discuz!NT數(shù)據(jù)庫(kù)讀寫(xiě)分離方案詳解
Discuz!NT這個(gè)產(chǎn)品在其企業(yè)版中提供了對(duì)‘讀寫(xiě)分離’機(jī)制的支持,使對(duì)CPU及內(nèi)存消耗嚴(yán)重的操作(CUD)被 分離到一臺(tái)或幾臺(tái)性能很高的機(jī)器上,而將頻繁讀取的操作(select)放到幾臺(tái)配置較低的機(jī)器上,然后通過(guò)‘事務(wù) 發(fā)布訂閱機(jī)制’,實(shí)現(xiàn)了在多個(gè)sqlserver數(shù)據(jù)庫(kù)之間快速高效同步數(shù)據(jù),從而達(dá)到了將‘讀寫(xiě)請(qǐng)求’按實(shí)際負(fù)載 情況進(jìn)行均衡分布的效果。2010-06-06
visual studio 2019正式版安裝簡(jiǎn)單教程
這篇文章主要為大家詳細(xì)介紹了visual studio 2019正式版安裝簡(jiǎn)單教程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06
asp.net 數(shù)據(jù)訪(fǎng)問(wèn)層基類(lèi)
阿楠收集自網(wǎng)絡(luò),打包下載,以下只截取其中一部分代碼。2009-03-03

