.NET 8项目中AuthenticationBuilder无AddOpenIdConnect方法的CS1061错误求助
.NET 8项目中AddOpenIdConnect方法引发CS1061错误的解决求助
在基于.NET 8的项目中编写认证配置代码时,遭遇CS1061错误:'AuthenticationBuilder' 不包含 'AddOpenIdConnect' 的定义,也找不到匹配的可访问扩展方法(提示可能缺少using指令或程序集引用)。多方搜索仍未解决,现将代码与错误详情附上,请求技术帮助。
项目代码
IConfiguration configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .Build(); var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); builder.Services.AddDbContext<StudentDataContext>(options => options.UseSqlServer(configuration.GetConnectionString("SecurityWebAppConnectionStrings"))); builder.Services.AddAuthorization(options => { options.AddPolicy("FacultyNumber", policy => policy.RequireClaim("FacultyNumber") ); }); builder.Services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = "oidc"; }) .AddCookie(options => { options.ExpireTimeSpan = TimeSpan.FromMinutes(60); options.Cookie.Name = "Cookie"; }) .AddOpenIdConnect("oidc", options => { options.Authority = "http://localhost:5000"; options.RequireHttpsMetadata = false; options.ClientId = "RouxAcademyMVC"; options.SaveTokens = true; }); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
错误详情
- 严重性:错误
- 代码:CS1061
- 描述:
'AuthenticationBuilder' 不包含 'AddOpenIdConnect' 的定义,也找不到接受类型为 'AuthenticationBuilder' 的第一个参数的可访问扩展方法 'AddOpenIdConnect'(是否缺少 using 指令或程序集引用?) - 项目:SecurityWebApp.WebClient
- 文件:SecurityWebApp.WebClient\Program.cs
- 行号:46
- 抑制状态:活动
解决方法
1. 安装必要的NuGet包
AddOpenIdConnect扩展方法来自Microsoft.AspNetCore.Authentication.OpenIdConnect包,需先安装:
- 使用.NET CLI命令:
dotnet add package Microsoft.AspNetCore.Authentication.OpenIdConnect - 或在NuGet包管理器中搜索并安装该包。
2. 添加using指令
在Program.cs文件顶部添加以下using语句:
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
3. 验证项目引用
安装完成后,检查项目依赖项,确保Microsoft.AspNetCore.Authentication.OpenIdConnect已成功添加,且版本与.NET 8兼容。
完成以上步骤后重新编译项目,CS1061错误即可解决。
内容的提问来源于stack exchange,提问作者mahdi yadi
相关产品推荐
相关产品推荐

