.NET Core 2.2升级3.0:AddOpenIdConnect方法替代方案咨询
.NET Core 3.0中替代AddOpenIdConnect的方案(Identity Server 4认证升级)
嘿,我刚好碰到过类似的.NET Core版本升级问题,给你捋捋怎么解决:
在.NET Core 3.0里,AddOpenIdConnect方法并没有消失,只是ASP.NET Core团队把认证相关的组件从核心框架中拆分出来,做成了独立的NuGet包。你只需要完成以下两步就能恢复原来的功能:
1. 安装对应的NuGet包
你需要手动安装Microsoft.AspNetCore.Authentication.OpenIdConnect包,版本要匹配.NET Core 3.0(比如3.0.0)。可以用下面的命令安装:
- 使用.NET CLI:
dotnet add package Microsoft.AspNetCore.Authentication.OpenIdConnect --version 3.0.0 - 使用Package Manager Console:
Install-Package Microsoft.AspNetCore.Authentication.OpenIdConnect -Version 3.0.0
2. 调整Startup.cs代码
你的原有代码逻辑基本不需要改动,只要确保在文件顶部引入正确的命名空间:
using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authentication.OpenIdConnect; using System;
修改后的ConfigureServices方法和你原来的代码几乎一致,完全兼容Identity Server 4的认证流程:
IServiceProvider ConfigureServices(IServiceCollection services) { services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(options => { options.SlidingExpiration = true; options.ExpireTimeSpan = TimeSpan.FromMinutes(60); }) .AddOpenIdConnect("oidc", options => { options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.Authority = "sso url"; options.RequireHttpsMetadata = false; options.ClientId = "client_id"; options.ClientSecret = "secret"; options.ResponseType = $"{OpenIdConnectParameterNames.Code} {OpenIdConnectParameterNames.IdToken}"; options.SaveTokens = true; options.GetClaimsFromUserInfoEndpoint = true; }); // 记得保留你原有返回ServiceProvider的逻辑 return services.BuildServiceProvider(); }
补充一句:这个官方的OpenID Connect包和Identity Server 4完全兼容,你不需要额外安装Identity Server专属的包,只要确保Authority地址配置正确就没问题。
内容的提问来源于stack exchange,提问作者Macko
相关产品推荐
相关产品推荐

