使用 JWT 进行 ASP.NET Core Web API 的身份验证与角色授权 本文介绍如何在 ASP.NET Core Web API 中实现 JSON Web Token (JWT) 身份验证与角色授权。通过创建用户登录、生成和验证 JWT 令牌,以及实现基于角色的访问控制,提升了应用程序的安全性。文章详细讲解了项目结构、关键类的实现以及中间件的配置,适合开发者快速上手。 JWT (1) 身份验证 (2) 角色授权 (1) ASP.NET Core (4) 中间件 (3) 项目结构 (1) 2024年9月20日 | 阅读 35
使用 SignalR 实现 ASP.NET Core 实时消息系统 本文介绍了如何在ASP.NET Core中使用SignalR实现实时消息系统。通过创建SignalR Hub、配置路由和前端连接,用户可以轻松实现服务器与多个客户端之间的实时消息通信。 SignalR (1) 实时通信 (1) ASP.NET (3) 聊天应用 (1) WebSockets (2) 消息推送 (1) 2024年9月17日 | 阅读 53
使用 Redis 在 .NET Core 中创建分布式锁 这篇文章介绍了如何在 .NET Core 中使用 Redis 创建分布式锁。通过示例代码展示了如何利用 Redis 的原子操作特性来管理跨进程的资源访问,同时涵盖了锁的获取、释放及处理未释放锁的情况。适合需要解决分布式系统资源竞争问题的开发者。 分布式锁 (1) Redis (1) .NET Core (6) 锁的获取 (1) 锁的释放 (1) 跨进程资源 (1) 2024年9月14日 | 阅读 23
ASP.NET Core 中的中间件:构建灵活的请求处理管道 在这篇文章中,我们探讨了 ASP.NET Core 中间件的核心概念及其在请求处理中的作用。中间件是处理请求和响应的重要组成部分,通过灵活的配置和排序,开发者可以在应用程序中实现认证、日志记录等功能。本文介绍了中间件的基本结构、实际示例以及如何有效地配置和排序中间件,以帮助您构建强大且灵活的 web 应用程序。 中间件 (3) 请求处理 (1) 日志记录 (4) 请求管道 (1) 配置 (5) 2024年9月13日 | 阅读 11
如何在控制台应用程序中实现 IConfiguration 依赖注入 在本文中,我们探讨了如何在 ASP.NET Core 控制台应用程序中实现 `IConfiguration` 依赖注入。通过扩展 `CreateServices` 方法,我们将 `IConfiguration` 添加到服务集合中,使得配置设置可以在应用程序中随处访问。示例代码展示了如何使用 `ConfigurationBuilder` 配置 JSON 文件,并通过构造函数注入 `IConfiguration` 实现配置的灵活管理。 ASP.NET Core (4) 控制台应用程序 (1) 配置设置 (1) 依赖注入 (2) `IConfiguration` (1) `ConfigurationBuilder` (1) 2024年9月13日 | 阅读 7
单例模式 (Singleton Pattern) 在 .NET Core 中的应用 在本篇文章中,我们深入探讨了单例模式(Singleton Pattern)在.NET Core应用中的重要性。单例模式确保一个类只有一个实例,并提供全局访问点,适用于管理共享资源如日志记录和配置设置。我们通过C#代码示例演示了如何实现这一模式,并讨论了其在配置管理、日志服务和数据库连接中的实际应用。 单例模式 (1) .NET Core (6) 共享资源 (1) 配置管理 (1) 日志服务 (1) 数据库连接 (1) 2024年9月13日 | 阅读 9
如何在 ASP.NET Core 8 中集成 Serilog 进行高效日志记录 本指南介绍如何在 ASP.NET Core 8 中集成 Serilog 实现高效的日志记录。涵盖 Serilog 的主要特性、配置步骤和实际应用示例,帮助开发者利用结构化日志提升应用程序的调试、监控和维护能力。 Serilog (2) 日志记录 (4) ASP.NET Core (4) 结构化日志 (1) 配置 (5) 中间件 (3) 2024年9月12日 | 阅读 13
使用分层架构构建.NET Core应用程序:产品和订单管理 本文介绍了如何在.NET Core中实现分层架构(Layered Architecture),通过构建产品和订单服务来展示各层的功能。包括展示层、应用层、业务逻辑层和数据访问层的详细实现,提供了代码示例和配置说明,帮助开发者理解如何通过分层架构提升应用的可维护性和扩展性。 分层架构 (1) N层架构 (1) .NET Core (6) C# (5) 产品服务 (1) 订单服务 (1) 2024年9月12日 | 阅读 10
.NET 通过 Serilog 和 Application Insights 简化 ASP.NET Core 日志记录 在本文中,我们探讨了如何在 ASP.NET Core 8 应用程序中通过 Serilog 和 Application Insights 实现高效的日志记录。Serilog 提供结构化日志记录,增强了调试和监控能力。我们详细介绍了从项目创建、包安装到配置 Serilog 和记录日志的步骤,帮助开发者提高应用程序的可追溯性和性能。 调试 (3) 日志记录 (4) 日志上下文 (1) NuGet 包 (1) Serilog (2) Web API (1) 2024年9月12日 | 阅读 11
正确使用 HttpClient 避免性能问题 在 C# 中,不当使用 `HttpClient` 可能导致性能问题,如套接字耗尽和服务器响应缓慢。频繁创建 `HttpClient` 实例会消耗大量资源。正确做法是使用单例 `HttpClient` 实例或 `IHttpClientFactory` 进行管理,以提高应用程序性能和稳定性。 高效使用 (1) HttpClient (1) 资源管理 (2) 单例 (1) 套接字耗尽 (1) TCP 状态 (1) 2024年9月12日 | 阅读 10