AspNetCore 2:用于 Web 的 OpenIdConnect Cookie,用于 API 的 JWT Bearer。是否可以

作者:编程家 分类: 编程代码 时间:2025-08-09

ASP.NET Core 2:使用 OpenIdConnect Cookie 用于 Web,使用 JWT Bearer 用于 API

在 ASP.NET Core 开发中,我们经常需要实现身份验证和授权的功能。其中,最常见的方案是使用 OpenIdConnect Cookie 用于 Web 应用程序,以及使用 JWT Bearer 用于 API。本文将介绍如何在 ASP.NET Core 2 中实现这两种身份验证方案,并提供相应的案例代码。

## 使用 OpenIdConnect Cookie 进行 Web 身份验证

OpenIdConnect 是一种基于 OAuth 2.0 的身份验证协议,它允许用户使用第三方身份提供者(如 Google、Facebook 等)进行登录。在 ASP.NET Core 中,我们可以使用 OpenIdConnect Middleware 来实现这一功能。

首先,我们需要在 Startup.cs 文件中配置 OpenIdConnect 服务。以下是一个简单的示例:

csharp

public void ConfigureServices(IServiceCollection services)

{

services.AddAuthentication(options =>

{

options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;

options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;

})

.AddCookie()

.AddOpenIdConnect(options =>

{

options.Authority = "https://accounts.google.com";

options.ClientId = "YOUR_CLIENT_ID";

options.ClientSecret = "YOUR_CLIENT_SECRET";

options.CallbackPath = "/signin-google";

options.Scope.Add("openid");

options.Scope.Add("email");

options.SaveTokens = true;

});

// 其他服务配置...

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{

// 其他中间件配置...

app.UseAuthentication();

// 其他配置...

}

在上述代码中,我们首先通过 services.AddAuthentication() 方法配置身份验证服务。默认情况下,我们使用 CookieAuthenticationDefaults.AuthenticationScheme 作为默认身份验证方案,并使用 OpenIdConnectDefaults.AuthenticationScheme 作为默认挑战方案。

接下来,通过 AddCookie() 方法添加 Cookie 认证服务,并通过 AddOpenIdConnect() 方法添加 OpenIdConnect 认证服务。在 OpenIdConnectOptions 中,我们可以配置一系列参数,包括身份提供者的地址、客户端 ID 和密钥、回调路径、授权范围等。

最后,在 Configure() 方法中调用 app.UseAuthentication() 来启用身份验证中间件。

## 使用 JWT Bearer 进行 API 身份验证

JWT(JSON Web Token)是一种轻量级的身份验证和授权机制,它使用 JSON 数据结构来传递信息,具有自包含性和可验证性。在 ASP.NET Core 中,我们可以使用 JWT Bearer Middleware 来实现基于 JWT 的 API 身份验证。

首先,我们需要在 Startup.cs 文件中配置 JWT Bearer 服务。以下是一个简单的示例:

csharp

public void ConfigureServices(IServiceCollection services)

{

// 其他服务配置...

services.AddAuthentication(options =>

{

options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

})

.AddJwtBearer(options =>

{

options.Authority = "https://api.example.com";

options.Audience = "YOUR_API_AUDIENCE";

options.TokenValidationParameters = new TokenValidationParameters

{

ValidateIssuer = true,

ValidateAudience = true,

ValidateLifetime = true,

ValidateIssuerSigningKey = true,

ValidIssuer = "https://api.example.com",

ValidAudience = "YOUR_API_AUDIENCE",

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("YOUR_SECRET_KEY"))

};

});

// 其他服务配置...

}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{

// 其他中间件配置...

app.UseAuthentication();

// 其他配置...

}

在上述代码中,我们通过 services.AddAuthentication() 方法配置身份验证服务,并使用 JwtBearerDefaults.AuthenticationScheme 作为默认身份验证方案。

接下来,通过 AddJwtBearer() 方法添加 JWT Bearer 认证服务。在 JwtBearerOptions 中,我们可以配置一系列参数,包括身份提供者的地址、API 的受众、令牌验证参数等。在这里,我们使用了一个对称密钥来验证令牌的签名。

最后,在 Configure() 方法中调用 app.UseAuthentication() 来启用身份验证中间件。

##

本文介绍了如何在 ASP.NET Core 2 中使用 OpenIdConnect Cookie 进行 Web 身份验证,以及使用 JWT Bearer 进行 API 身份验证。通过配置相应的中间件和参数,我们可以轻松地实现身份验证和授权的功能。希望这篇文章对你有所帮助!

代码案例:

csharp

// Web 身份验证

services.AddAuthentication(options =>

{

options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;

options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;

})

.AddCookie()

.AddOpenIdConnect(options =>

{

options.Authority = "https://accounts.google.com";

options.ClientId = "YOUR_CLIENT_ID";

options.ClientSecret = "YOUR_CLIENT_SECRET";

options.CallbackPath = "/signin-google";

options.Scope.Add("openid");

options.Scope.Add("email");

options.SaveTokens = true;

});

// API 身份验证

services.AddAuthentication(options =>

{

options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;

})

.AddJwtBearer(options =>

{

options.Authority = "https://api.example.com";

options.Audience = "YOUR_API_AUDIENCE";

options.TokenValidationParameters = new TokenValidationParameters

{

ValidateIssuer = true,

ValidateAudience = true,

ValidateLifetime = true,

ValidateIssuerSigningKey = true,

ValidIssuer = "https://api.example.com",

ValidAudience = "YOUR_API_AUDIENCE",

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("YOUR_SECRET_KEY"))

};

});

以上就是关于使用 OpenIdConnect Cookie 进行 Web 身份验证和使用 JWT Bearer 进行 API 身份验证的介绍。希望对你有所帮助!