.NET CORE 认证模块-注册方案与请求认证探究

发布时间:2026/9/11 16:27:57

.NET CORE 认证模块-注册方案与请求认证探究 .NET CORE 认证模块-注册方案与请求认证探究一、认证模块的基础认知在 .NET Core 中认证模块是一个核心的安全组件它负责验证用户的身份。认证过程包括两个关键阶段注册方案和请求认证。注册方案定义了认证的方式如 Cookie、JWT 等而请求认证则是在每次 HTTP 请求中验证用户的身份。首先我们需要理解几个核心概念-AuthenticationHandler认证处理器负责具体的认证逻辑-AuthenticationScheme认证方案定义了认证的名称和类型-AuthenticationMiddleware认证中间件处理 HTTP 请求中的认证流程## 二、注册方案从基础开始注册方案是认证模块的起点。在Startup.cs的ConfigureServices方法中我们可以注册各种认证方案。最简单的例子是使用 Cookie 认证。### 基础 Cookie 认证注册csharp// 在 Startup.cs 的 ConfigureServices 方法中public void ConfigureServices(IServiceCollection services){ // 添加认证服务并注册默认的 Cookie 方案 services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options { // 设置登录路径当用户未认证时重定向到此路径 options.LoginPath /Account/Login; // 设置 Cookie 名称 options.Cookie.Name MyAppCookie; // 设置 Cookie 过期时间 options.ExpireTimeSpan TimeSpan.FromHours(1); }); services.AddControllersWithViews();}这段代码做了以下事情1.AddAuthentication()注册认证服务并指定默认方案为 Cookie2.AddCookie()注册 Cookie 方案并配置选项3. 配置登录路径和 Cookie 行为## 三、请求认证中间件的作用注册方案后我们需要在请求管道中使用认证中间件。在Configure方法中我们添加认证中间件来处理每个请求。### 请求认证配置csharp// 在 Startup.cs 的 Configure 方法中public void Configure(IApplicationBuilder app, IWebHostEnvironment env){ if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(/Home/Error); } app.UseStaticFiles(); // 添加认证中间件 - 顺序很重要必须在 UseAuthorization 之前 app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints { endpoints.MapControllerRoute( name: default, pattern: {controllerHome}/{actionIndex}/{id?}); });}关键点-UseAuthentication()必须放在UseAuthorization()之前- 中间件的顺序决定了请求处理的流程## 四、深入探究自定义认证方案当标准方案不满足需求时我们可以创建自定义认证方案。下面是一个完整的示例展示如何创建基于 API Key 的认证方案。### 自定义认证处理器实现csharp// 自定义认证方案类public class ApiKeyAuthenticationHandler : AuthenticationHandlerApiKeyAuthenticationOptions{ private readonly IConfiguration _configuration; public ApiKeyAuthenticationHandler( IOptionsMonitorApiKeyAuthenticationOptions options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IConfiguration configuration) : base(options, logger, encoder, clock) { _configuration configuration; } protected override async TaskAuthenticateResult HandleAuthenticateAsync() { // 从请求头中获取 API Key if (!Request.Headers.TryGetValue(X-API-Key, out var apiKeyHeaderValues)) { // 如果没有 API Key返回认证失败 return AuthenticateResult.Fail(Missing API Key); } var providedApiKey apiKeyHeaderValues.FirstOrDefault(); if (string.IsNullOrEmpty(providedApiKey)) { return AuthenticateResult.Fail(Invalid API Key); } // 验证 API Key 是否有效这里简单地与配置文件中的值比较 var validApiKey _configuration[ApiKey]; if (!string.Equals(providedApiKey, validApiKey, StringComparison.Ordinal)) { return AuthenticateResult.Fail(Invalid API Key); } // 创建 Claims 和身份标识 var claims new[] { new Claim(ClaimTypes.Name, API User), new Claim(ClaimTypes.Role, ApiClient), new Claim(ApiKey, providedApiKey) }; var identity new ClaimsIdentity(claims, Scheme.Name); var principal new ClaimsPrincipal(identity); var ticket new AuthenticationTicket(principal, Scheme.Name); // 返回认证成功结果 return AuthenticateResult.Success(ticket); }}### 自定义认证选项类csharp// 自定义认证选项public class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions{ public const string DefaultScheme ApiKey; public string Scheme DefaultScheme;}### 在 Startup 中注册自定义方案csharp// 在 ConfigureServices 方法中public void ConfigureServices(IServiceCollection services){ // 注册自定义 API Key 认证方案 services.AddAuthentication(ApiKeyAuthenticationOptions.DefaultScheme) .AddSchemeApiKeyAuthenticationOptions, ApiKeyAuthenticationHandler( ApiKeyAuthenticationOptions.DefaultScheme, options { }); services.AddControllers();}## 五、高级应用多方案认证与策略在实际项目中我们可能需要支持多种认证方式。.NET Core 支持多方案认证并允许通过策略进行细粒度控制。### 多方案认证配置csharppublic void ConfigureServices(IServiceCollection services){ services.AddAuthentication() .AddCookie(CookieAuth, options { options.LoginPath /Account/Login; options.Cookie.Name MyAppCookie; }) .AddJwtBearer(Bearer, options { options.TokenValidationParameters new TokenValidationParameters { ValidateIssuer true, ValidIssuer https://myapp.com, ValidateAudience true, ValidAudience https://myapp.com, ValidateLifetime true, IssuerSigningKey new SymmetricSecurityKey( Encoding.UTF8.GetBytes(my-secret-key-here)) }; }); // 定义认证策略 services.AddAuthorization(options { // 策略1只能通过 Cookie 认证 options.AddPolicy(CookieOnly, policy { policy.AuthenticationSchemes.Add(CookieAuth); policy.RequireAuthenticatedUser(); }); // 策略2Cookie 或 JWT 都可以 options.AddPolicy(Mixed, policy { policy.AuthenticationSchemes.Add(CookieAuth); policy.AuthenticationSchemes.Add(Bearer); policy.RequireAuthenticatedUser(); }); }); services.AddControllers();}### 在控制器中使用策略csharp[ApiController][Route(api/[controller])]public class SecureController : ControllerBase{ // 使用默认认证方案 [HttpGet(public-data)] [Authorize] public IActionResult GetPublicData() { return Ok(This is public data); } // 使用 Cookie 认证策略 [HttpGet(cookie-data)] [Authorize(Policy CookieOnly)] public IActionResult GetCookieData() { return Ok(This requires Cookie authentication); } // 使用混合认证策略 [HttpGet(mixed-data)] [Authorize(Policy Mixed)] public IActionResult GetMixedData() { return Ok(This accepts both Cookie and JWT); }}## 六、性能优化与最佳实践### 认证缓存优化对于频繁的 API 请求我们可以缓存认证结果以提高性能csharppublic class CachedAuthenticationHandler : AuthenticationHandlerAuthenticationSchemeOptions{ private readonly IMemoryCache _cache; public CachedAuthenticationHandler( IOptionsMonitorAuthenticationSchemeOptions options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IMemoryCache cache) : base(options, logger, encoder, clock) { _cache cache; } protected override async TaskAuthenticateResult HandleAuthenticateAsync() { var token Request.Headers[Authorization].FirstOrDefault(); if (string.IsNullOrEmpty(token)) return AuthenticateResult.Fail(No token); // 尝试从缓存获取认证结果 var cacheKey $auth_{token}; if (_cache.TryGetValue(cacheKey, out AuthenticateResult cachedResult)) { return cachedResult; } // 执行实际的认证逻辑 var result await PerformAuthenticationAsync(token); // 缓存认证结果设置过期时间 if (result.Succeeded) { _cache.Set(cacheKey, result, TimeSpan.FromMinutes(5)); } return result; } private TaskAuthenticateResult PerformAuthenticationAsync(string token) { // 实际的认证逻辑 return Task.FromResult(AuthenticateResult.Fail(Not implemented)); }}## 总结通过本文的探究我们深入理解了 .NET Core 认证模块的核心机制。从基础的 Cookie 认证注册到请求认证中间件的配置再到自定义认证方案的实现每一步都揭示了认证模块的灵活性和可扩展性。关键要点1.注册方案是认证的起点通过AddAuthentication和AddCookie/AddJwtBearer等方法配置2.请求认证通过UseAuthentication中间件实现顺序至关重要3.自定义方案允许我们根据业务需求创建独特的认证逻辑4.多方案与策略提供了细粒度的访问控制能力5.性能优化如缓存认证结果可以提升系统响应速度在实际开发中应根据应用场景选择合适的认证方案并注意安全性和性能的平衡。掌握这些知识将帮助你在 .NET Core 应用中构建安全、高效的认证系统。
延伸阅读

更多相关文章

2026/9/11 16:27:44

Windows 7系统核心功能与优化全解析

1. Windows 7操作系统概述Windows 7作为微软公司2009年发布的经典操作系统,至今仍在许多企业和个人电脑中广泛使用。相比前代Vista系统,它在性能优化、用户界面和稳定性方面都有显著提升。我使用Win7系统长达8年时间,从日常办公到专业软件运行…

2026/9/11 17:26:53

Allure 1常见问题解决:15个你必须知道的技巧

Allure 1常见问题解决:15个你必须知道的技巧 【免费下载链接】allure1 Allure 1 isnt supported any more, please consider using Allure 2 https://github.com/allure-framework/allure2 instead 项目地址: https://gitcode.com/gh_mirrors/al/allure1 All…

2026/9/10 12:18:33

如何在PHP项目中快速集成highlight.php?5分钟上手教程

如何在PHP项目中快速集成highlight.php?5分钟上手教程 【免费下载链接】highlight.php A port of highlight.js by Ivan Sagalaev to PHP 项目地址: https://gitcode.com/gh_mirrors/hi/highlight.php highlight.php是一款基于PHP开发的服务器端语法高亮工具…

2026/9/11 17:28:05

macOS 安装 OpenCV 指南:从零到跑通第一段视觉代码

macOS 安装 OpenCV 指南:从零到跑通第一段视觉代码 【免费下载链接】opencv Open Source Computer Vision Library 项目地址: https://gitcode.com/GitHub_Trending/opencv31/opencv 写代码调 import cv2 却直接报 ModuleNotFoundError,项目卡在这…

2026/9/11 17:28:04

2026年7月福州市新房价格深度分析报告

一、报告摘要本报告基于2026年7月福州市新房实际成交案例,从成交价格、区域分布、户型结构、购房人群特征等维度进行深度分析,揭示当前福州新房市场的真实价格水平与走势,为购房者、开发商及研究机构提供数据参考。二、数据来源与样本说明本报…

2026/9/11 17:23:04

Windows 部署大模型 不联网 本地离线推理

Ollama:本地模型运行引擎 代码模型 (根据自身需求选用) 此处 以 qwen2.5-coder:7b-instruct 为例 (后来需要不联网翻译一些资料,用的是 translategemma:4b模型) Open WebUI:本地网页界面&am…

2026/9/10 16:39:38

超人会飞不算本事:系统稳定依赖清晰规则与边界设计

开头先不绕弯子。“#斯坦李吐槽dc 所以超人是无缘无故会飞的嘛哈哈哈哈哈哈哈锤哥真是技术人才啊!#雷神 #复联”这类调侃式短标题,第一波冲击力在于它把两个宇宙的角色塞进同一个吐槽箱里,但细想一下就能发现,它真正碰到的根本不是…

2026/9/10 11:16:38

超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论

把“蜘蛛侠 vs 超人”放在 CSDN 上聊,可能很多人第一反应是走错片场了。但如果把这两个角色看成“两个持续运营了 80 多年的文化产品”,你会发现,这场比较本质上是两个不同 IP 策略的长期结果对比:超人赢在定义了整个超级英雄题材…

2026/9/9 16:31:09

基于CNN的调制信号识别:MATLAB实现时频图分类实战

简介:本资源是一套面向通信工程与信号处理方向学习者、研究者的深度学习实践方案,聚焦调制信号自动检测与识别这一典型无线通信任务,解决传统方法依赖人工特征、低信噪比下性能下降等痛点。压缩包共12个文件(10.73MB)&…

2026/9/10 12:32:02

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/10 15:19:50

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/10 15:49:53

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
咨询二维码