在.NET Core中想給API進(jìn)行安全認(rèn)證,最簡(jiǎn)單的無(wú)非就是Jwt,悠然記得一年前寫(xiě)的Jwt Demo,現(xiàn)在拿回來(lái)改成.NET Core的,但是在編碼上的改變并不大,因?yàn)镴wt已經(jīng)足夠強(qiáng)大了。在項(xiàng)目中分為 DotNetCore_Jwt_Server 以及 DotNetCore_Jwt_Client ,從名字就可以看出來(lái)是啥意思,博客園高手云集,我就不多訴說(shuō),這篇博客就當(dāng)是一篇記錄。 當(dāng)然本案例是Server&Client雙項(xiàng)目,如果你要合成自己發(fā)證的形式,那你就自己改下代碼玩。 在Server層都會(huì)有分發(fā)Token的服務(wù),在其中做了用戶密碼判斷,隨后根據(jù) Claim 生成 jwtToken 的操作。 其生成Token的服務(wù)代碼: namespace DotNetCore_Jwt_Server.Services {public interface ITokenService {string GetToken(User user); }public class TokenService : ITokenService {private readonly JwtSetting _jwtSetting;public TokenService(IOptions option) { _jwtSetting = option.Value; }public string GetToken(User user) {//創(chuàng)建用戶身份標(biāo)識(shí),可按需要添加更多信息var claims = new Claim[] {new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),new Claim("id", user.Id.ToString(), ClaimValueTypes.Integer32),new Claim("name", user.Name),new Claim("admin", user.IsAdmin.ToString(),ClaimValueTypes.Boolean) };//創(chuàng)建令牌var token = new JwtSecurityToken( issuer: _jwtSetting.Issuer, audience: _jwtSetting.Audience, signingCredentials: _jwtSetting.Credentials, claims: claims, notBefore: DateTime.Now, expires: DateTime.Now.AddSeconds(_jwtSetting.ExpireSeconds) );string jwtToken = new JwtSecurityTokenHandler().WriteToken(token);return jwtToken; } } } 在獲取Token中我們依賴注入服務(wù)到控制器中,隨后依賴它進(jìn)行認(rèn)證并且分發(fā)Token, public class ValuesController : ControllerBase {private readonly IUserService _userService;private readonly ITokenService _tokenService;public ValuesController(IUserService userService, ITokenService tokenService) { _userService = userService; _tokenService = tokenService; } [HttpGet]public async Task Get() {await Task.CompletedTask;return "Welcome the Json Web Token Solucation!"; } [HttpGet("getToken")]public async Task GetTokenAsync(string name, string password) {var user = await _userService.LoginAsync(name, password);if (user == null)return "Login Failed";var token = _tokenService.GetToken(user);var response = new{ Status = true, Token = token, Type = "Bearer"};return JsonConvert.SerializeObject(response); } } 隨后,我們又在項(xiàng)目配置文件中填寫(xiě)了幾個(gè)字段,相關(guān)備注已注釋,但值得說(shuō)明的是有位朋友問(wèn)我,服務(wù)器端生成的Token不需要保存嗎,比如Redis或者是Session,其實(shí)Jwt Token是無(wú)狀態(tài)的,他們之間的對(duì)比第一個(gè)是你的token解密出來(lái)的信息正確與否,第二部則是看看你 SecurityKey 是否正確,就這樣他們的認(rèn)證才會(huì)得出結(jié)果。 "JwtSetting": {"SecurityKey": "d0ecd23c-dfdb-4005-a2ea-0fea210c858a", // 密鑰"Issuer": "jwtIssuertest", // 頒發(fā)者"Audience": "jwtAudiencetest", // 接收者"ExpireSeconds": 20000 // 過(guò)期時(shí)間 } 隨后我們需要DI兩個(gè)接口以及初始化設(shè)置相關(guān)字段。 public void ConfigureServices(IServiceCollection services) { services.Configure(Configuration.GetSection("JwtSetting")); services.AddScoped(); services.AddScoped(); services.AddControllers(); } 在Client中,我一般會(huì)創(chuàng)建一個(gè)中間件用于接受認(rèn)證結(jié)果,AspNetCore Jwt 源碼中給我們提供了中間件,我們?cè)谶M(jìn)一步擴(kuò)展,其源碼定義如下: Task AuthenticateAsync( HttpContext context, scheme) =>().AuthenticateAsync(context, scheme); } 其該擴(kuò)展會(huì)返回一個(gè) AuthenticateResult 類型的結(jié)果,其定義部分是這樣的,我們就可以將計(jì)就計(jì),給他來(lái)個(gè)連環(huán)套。 ![]() 連環(huán)套直接接受 httpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme) 返回回來(lái)的值,隨后進(jìn)行判斷返回相應(yīng)的Http響應(yīng)碼。 public class AuthMiddleware {private readonly RequestDelegate _next;public AuthMiddleware(RequestDelegate next) { _next = next; }public async Task Invoke(HttpContext httpContext) {var result = await httpContext.AuthenticateAsync(JwtBearerDefaults.AuthenticationScheme);if (!result.Succeeded) { httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;await httpContext.Response.WriteAsync("Authorize error"); }else{ httpContext.User = result.Principal;await _next.Invoke(httpContext); } } } 當(dāng)然你也得在Client中添加認(rèn)證的一些設(shè)置,它和Server端的 IssuerSigningKey 一定要對(duì)應(yīng),否則認(rèn)證失敗。 public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddScoped();var jwtSetting = new JwtSetting(); Configuration.Bind("JwtSetting", jwtSetting); services.AddCors(options =>{ options.AddPolicy("any", builder =>{ builder.AllowAnyOrigin() //允許任何來(lái)源的主機(jī)訪問(wèn) .AllowAnyMethod() .AllowAnyHeader(); }); }); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = jwtSetting.Issuer, ValidAudience = jwtSetting.Audience, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSetting.SecurityKey)), 默認(rèn) 300s ClockSkew = TimeSpan.Zero }; }); services.AddControllers(); } 隨后,你就可以編寫(xiě)帶需認(rèn)證才可以訪問(wèn)的API了,如果認(rèn)證失敗則會(huì)返回401的錯(cuò)誤響應(yīng)。 [Route("api/[controller]")] [ApiController]public class ValuesController : ControllerBase {private readonly IIdentityService _identityService;public ValuesController(IIdentityService identityService) { _identityService = identityService; } [HttpGet] [Authorize]public async Task Get() {await Task.CompletedTask;return $"{_identityService.GetUserId()}:{_identityService.GetUserName()}"; } 值得一提的是,我們可以根據(jù) IHttpContextAccessor 以來(lái)注入到我們的Service或者Api中,它是一個(gè)當(dāng)前請(qǐng)求的認(rèn)證信息上下文,這將有利于你獲取用戶信息去做該做的事情。 public class IdentityService : IIdentityService {private readonly IHttpContextAccessor _context;public IdentityService(IHttpContextAccessor context) { _context = context; }public int GetUserId() {var nameId = _context.HttpContext.User.FindFirst("id");return nameId != null ? Convert.ToInt32(nameId.Value) : 0; }public string GetUserName() {return _context.HttpContext.User.FindFirst("name")?.Value; } } 在源碼中該類的定義如下,實(shí)際上我們可以看到只不過(guò)是判斷了當(dāng)前的http上下文吧,所以我們得出,如果認(rèn)證失敗,上下本信息也是空的。 public class HttpContextAccessor : IHttpContextAccessor {private static AsyncLocal _httpContextCurrent = new AsyncLocal();public HttpContext HttpContext {get{return _httpContextCurrent.Value?.Context; }set{var holder = _httpContextCurrent.Value;if (holder != null) {// Clear current HttpContext trapped in the AsyncLocals, as its done.holder.Context = null; }if (value != null) {// Use an object indirection to hold the HttpContext in the AsyncLocal,// so it can be cleared in all ExecutionContexts when its cleared._httpContextCurrent.Value = new HttpContextHolder { Context = value }; } } }private class HttpContextHolder {public HttpContext Context; } } 如果要通過(guò)js來(lái)測(cè)試代碼,您可以添加請(qǐng)求頭來(lái)進(jìn)行認(rèn)證,beforeSend是在請(qǐng)求之前的事件。 beforeSend : function(request) { request.setRequestHeader("Authorization", sessionStorage.getItem("Authorization")); } 好了,今天就說(shuō)到這,代碼地址在https://github.com/zaranetCore/DotNetCore_Jwt 中。 ![]() |
|