1. 程式人生 > 程式設計 >.NET 6實現基於JWT的Identity功能方法詳解

.NET 6實現基於JWT的Identity功能方法詳解

目錄
  • 需求
  • 目標
  • 原理與思路
  • 實現
    • 引入Identity元件
    • 新增認證服務
    • 使用JWT認證和定義授權方式
    • 引入認證授權中介軟體
    • 新增JWT配置
    • 增加認證使用者Model
    • 實現認證服務CreateToken方法
    • 新增認證介面
    • 保護API資源
  • 驗證
    • 驗證1: 驗證直接訪問建立TodoList介面
    • 驗證2: 獲取Token
    • 驗證3: 攜帶Token訪問建立TodoList介面
    • 驗證4: 更換Policy
  • 一點擴充套件
    • 總結
      • 參考資料

        需求

        在.NET Web API開發中還有一個很重要的需求是關於身份認證和授權的,這個主題非常大,所以本文不打算面面俱到地介紹整個主題,而僅使用.NET框架自帶的認證和授權中介軟體去實現基於JWT的身份認證和授權功能。一些關於這個主題的基本概念也不會花很多的篇幅去講解,我們還是專注在實現上。

        目標

        TodoList專案增加身份認證和授權功能。

        原理與思路

        為了實現身份認證和授權功能,我們需要使用.NET自帶的AuthenticationAuthorization元件。在本文中我們不會涉及Identity Server的相關內容,這是另一個比較大的主題,因為許可證的變更,Identity Server 4將不再能夠免費應用於盈利超過一定限額的商業應用中,詳情見官網IdentityServer。微軟同時也在將廣泛使用的IdentityServer的相關功能逐步整合到框架中:ASP.NET Core 6 and Authentication Servers,在本文中同樣暫不會涉及。

        實現

        引入Identity元件

        我們在Infrastructure專案中新增以下Nuget包:

        <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
        <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="6.0.1" />
        

        並新建Identity目錄用於存放有關認證和授權的具體功能,首先新增使用者類ApplicationUser

        ApplicationUser.cs

        using Microsoft.AspNetCore.Identity;
        
        namespace TodoList.Infrastructure.Identity;
        
        public class ApplicationUser : IdentityUser
        {
            // 不做定製化實現,僅使用原生功能
        }
        

        由於我們希望使用現有的SQL Server來儲存認證相關的資訊,所以還需要修改DbContext:

        TodoListDbContext.cs

        public class TodoListDbContext : IdentityDbContext<ApplicationUser>
        {
            private readonly IDomainEventService _domainEventService;
        
            public TodoListDbContext(
                DbContextOptions<TodoListDbContext> options,IDomainEventService domainEventService) : base(options)
            {
                _domainEventService = domainEventService;
            }
            // 省略其他...
        }
        

        為了後面演示的方便,我們還可以在新增種子資料的邏輯裡增加內建使用者資料:

        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))
            {
                // 建立的使用者名稱為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>>();
                    // 生成內建使用者
                    await TodoListDbContextSeed.SeedDefaultUserAsync(userManager,roleManager);
                    // 省略其他...
                }
                catch (Exception ex)
                {
                    throw new Exception($"An error occurred migrating the DB: {ex.Message}");
                }
            }
        }
        

        最後我們需要來修改DependencyInjection部分,以引入身份認證和授權服務:

        DependencyInjection.cs

        // 省略其他....
        // 配置認證服務
        // 配置認證服務
        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();
        

        新增認證服務

        Applicaiton/Common/Interfaces中新增認證服務介面IIdentityService

        IIdentityService.cs

        namespace TodoList.Application.Common.Interfaces;
        
        public interface IIdentityService
        {
            // 出於演示的目的,只定義以下方法,實際使用的認證服務會提供更多的方法
            Task<string> CreateUserAsync(string userName,string password);
            Task<bool> ValidateUserAsync(UserForAuthentication userForAuthentication);
            Task<string> CreateTokenAsync();
        }
        

        然後在Infrastructure/Identity中實現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,usehttp://www.cppcns.comrForAuthentication.Password);
                if (!result)
                {
                    _logger.LogWarning($"{nameof(ValidateUserAsync)}: Authentication failed. Wrong username or password.");
                }
        
                return result;
            }
        
            public async Task<string> CreateTokenAsync()
            {
                // 暫時還不來實現這個方法
                throw new NotImplementedException();
            }
        }
        

        並在DependencyInjection中進行依賴注入:

        DependencyInjection.cs

        // 省略其他...
        // 注入認證服務
        services.AddTransient<IIdentityService,IdentityService>();
        

        現在我們來回顧一下已經完成的部分:我們配置了應用程式使用內建的Identity服務並使其使用已有的資料庫儲存;我們生成了種子使用者資料;還實現了認證服務的功能。

        在繼續下一步之前,我們需要對資料庫做一次Migration,使認證鑑權相關的資料表生效:

        .NET 6實現基於JWT的Identity功能方法詳解

        $ 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
        # 建立相關資料表...
        [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=[],CommandTimeout='30']
        INSERT INTO [__EFMigrationsHistory] ([MigrationId],[ProductVersion])
        VALUES (N'20220108060343_AddIdentities',N'6.0.1');
        Done.
        

        執行Api程式,然後去資料庫確認一下生成的資料表:

        .NET 6實現基於JWT的Identity功能方法詳解

        種子使用者:

        .NET 6實現基於JWT的Identity功能方法詳解

        以及角色:

        .NET 6實現基於JWT的Identity功能方法詳解

        到目前為止,我已經集成了Identity框架,接下來我們開始實現基於JWT的認證和API的授權功能:

        使用JWT認證和定義授權方式

        Infrastructure專案的DependencyInjection中新增JWT認證配置:

        DependencyInjection.cs

        // 省略其他...
        // 新增認證方法為JWT Token認證
        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成了硬編碼的字串,實際環境中,最好是需要從環境變數中進行獲取,而不應該寫在程式碼中
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("SECRET") ?? "TodoListApiSecretKey"))
                };
            });
        
        // 新增授權Policy是基於角色的,策略名稱為OnlyAdmin,策略要求具有Administrator角色
        services.AddAuthorization(options => 
            options.AddPolicy("OnlyAdmin",policy => policy.RequireRole("Administrator")));
        

        引入認證授權中介軟體

        Api專案的Program中,MapControllers上面引入:

        Program.cs

        // 省略其他...
        app.UseAuthentication();
        app.UseAuthorization();
        

        新增JWT配置

        appsettings.Development.on

        "JwtSettings": {
            "validIssuer": "TodoListApi","validAudience": "http://localhost:5050","expires": 5
        }
        

        增加認證使用者Model

        Application/Common/Models中新增用於使用者認證的型別:

        UserForAuthentication.cs

        using System.ComponentModel.DataAnnotations;
        
        namespace TodoList.Application.Common.Models;
        
        public record UserForAuthentication
        {
            [Required(ErrorMessage = "username is required")]
            public string? UsYlcLUJuEBerName { get; set; }
        
            [Required(ErrorMessage = "password is required")]
            public string? Password { get; set; }
        }
        

        實現認證服務CreateToken方法

        因為本篇文章我們沒有使用整合的IdentityServer元件,而是應用程式自己去發放Token,那就需要我們去實現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成了硬編碼的字串,實際環境中,最好是需要從環境變數中進行獲取,而不應該寫在程式碼中
            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()
        {
            // 演示了返回使用者名稱和Role兩類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選項
            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;
        }
        

        新增認證介面

        Api專案中新建一個Controller用於實現獲取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() });
            }
        }
        

        保護API資源

        我們準備使用建立TodoList介面來演示認證和授權功能,所以新增屬性如下:

        // 省略其他...
        [HttpPost]
        // 演示使用Policy的授權
        [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));
        }
        

        驗證

        驗證1: 驗證直接訪問建立TodoList介面

        啟動Api專案,直接執行建立TodoList的請求:

        .NET 6實現基於JWT的Identity功能方法詳解

        得到了401 Unauthorized結果。

        驗證2: 獲取Token

        請求獲取Token的介面:

        .NET 6實現基於JWT的Identity功能方法詳解

        可以看到我們已經拿到了JWT Token,把這個Token放到JWT解析一下可以看到:

        .NET 6實現基於JWT的Identity功能方法詳解

        主要在payload中可以看到兩個Claims和其他配置的資訊。

        驗證3: 攜帶Token訪問建立TodoList介面

        選擇Bearer Token驗證方式並填入獲取到的Token,再次請求建立TodoList:

        .NET 6實現基於JWT的Identity功能方法詳解

        驗證4: 更換Policy

        修改Infrastructure/DependencyInjection.cs

        // 省略其他...
        // 新增授權Policy是基於角色的,策略名稱為OnlyAdmin,策略要求具有Administrator角色
        services.AddAuthorization(options =>
        {
            options.AddPolicy("OnlyAdmin",policy => policy.RequireRole("Administrator"));
            options.AddPolicy("OnlySuper",policy => policy.RequireRole("SuperAdmin"));
        });
        

        並修改建立TodoList介面的授權Policy:

        // 省略其他...
        [Authorize(Policy = "OnlySuper")]
        

        還是使用admin@locahost使用者的使用者名稱和密碼獲取最新的Token後,攜帶Token請求建立新的TodoList

        .NET 6實現基於JWT的Identity功能方法詳解

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

        .NET 6實現基於JWT的Identity功能方法詳解

        告訴我們需要一個具有SuperAdmin角色的使用者的合法Token才會被授權。

        那麼到此為止,我們已經實現了基於.NET自帶的Identity框架,發放Token,完成認證和授權的功能。

        一點擴充套件

        關於在.NET Web API專案中進行認證和授權的主題非常龐大,首先是認證的方式可以有很多種,除了我們在本文中演示的基於JWT Token的認證方式以外,還有OpenId認證,基於Azure Active Directory的認證,基於OAuth協議的認證等等;其次是關於授權的方式也有很多種,可以是基於角色的授權,可以是基於Claims的授權,可以是基於Policy的授權,也可以自定義更多的授權方式。然後是具體的授權伺服器的實現,有基於Identity Server 4的實現,當然在其更改過協議後,我們可以轉而使用.NET中移植進來的IdentityServer元件實現,配置的方式也有很多。

        由於IdentityServer涉及的知識點過於龐雜,所以本文並沒有試圖全部講到,考慮後面單獨出一個系列來講關於IdentityServer.NET 6 Web API開發中的應用。

        總結

        在本文中,我們實現了基於JWT Token的認證和授權。下一篇文章我們來看看為什麼需要以及如何實現Refresh Token機制。

        參考資料

        IdentityServer

        ASP.NET Core 6 and Authentication Servers

        以上就是.NET 6實現基於JWT的Identity功能方法詳解的詳細內容,更多關於.NET 6基於JWT的Identity功能的資料請關注我們其它相關文章!