gpt4 book ai didi

c# - ASP.NET Core 2.0 - 必须提供 ArgumentException : Options. ClientId

转载 作者:太空狗 更新时间:2023-10-29 21:32:17 24 4
gpt4 key购买 nike

我有一个 .NET Core 1.1 应用程序,我希望升级到 .NET Core 2.0。更新目标框架和所有依赖项后,我发现我的身份验证设置无法编译。我已更新以说明已删除的属性和已弃用/已移动的方法调用。省略号用于表示为简洁起见而省略的代码。

每当我启动我的应用程序时,我现在都会收到以下错误 enter image description here

1.1 代码 - Startup.cs 的 public void Configure() 方法内部

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies",
ExpireTimeSpan = TimeSpan.FromHours(12),
SlidingExpiration = false,
CookiePath = CookiePath,
CookieName = "MyCookie"
});

var openIdConnectionOptions = new OpenIdConnectOptions
{
ClientId = Configuration["OpenIdSettings:ClientId"],
ClientSecret = Configuration["OpenIdSettings:ClientSecret"],
Authority = Configuration["OpenIdSettings:Authority"],
MetadataAddress = $"{Configuration["OpenIdSettings:Authority"]}/.well-known/openid-configuration",
GetClaimsFromUserInfoEndpoint = true,
AuthenticationScheme = "oidc",
SignInScheme = "Cookies",
ResponseType = OpenIdConnectResponseType.IdToken,
TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
// This sets the value of User.Identity.Name to users AD username
NameClaimType = IdentityClaimTypes.WindowsAccountName,
RoleClaimType = IdentityClaimTypes.Role,
AuthenticationType = "Cookies",
ValidateIssuer = false
}
};

// Scopes needed by application
openIdConnectionOptions.Scope.Add("openid");
openIdConnectionOptions.Scope.Add("profile");
openIdConnectionOptions.Scope.Add("roles");

app.UseOpenIdConnectAuthentication(openIdConnectionOptions);

我正在阅读的所有内容都表明此过程已移至 ConfigureServices 方法。这是我的 Core 2.0 新代码

public void ConfigureServices(IServiceCollection services)
{
...

services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(options => new CookieAuthenticationOptions
{
//AuthenticationScheme = "Cookies", // Removed in 2.0
ExpireTimeSpan = TimeSpan.FromHours(12),
SlidingExpiration = false,
Cookie = new CookieBuilder
{
Path = CookiePath,
Name = "MyCookie"
}
}).AddOpenIdConnect(options => GetOpenIdConnectOptions());

...
}

public void Configure(IApplicationBuilder app)
{
...
app.UseAuthentication();
...
}
private OpenIdConnectOptions GetOpenIdConnectOptions()
{
var openIdConnectionOptions = new OpenIdConnectOptions
{
ClientId = Configuration["OpenIdSettings:ClientId"],
ClientSecret = Configuration["OpenIdSettings:ClientSecret"],
Authority = Configuration["OpenIdSettings:Authority"],
MetadataAddress = $"{Configuration["OpenIdSettings:Authority"]}/.well-known/openid-configuration",
GetClaimsFromUserInfoEndpoint = true,
SignInScheme = "Cookies",
ResponseType = OpenIdConnectResponseType.IdToken,

TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
// This sets the value of User.Identity.Name to users AD username
NameClaimType = IdentityClaimTypes.WindowsAccountName,
RoleClaimType = IdentityClaimTypes.Role,
AuthenticationType = "Cookies",
ValidateIssuer = false
}
};

// Scopes needed by application
openIdConnectionOptions.Scope.Add("openid");
openIdConnectionOptions.Scope.Add("profile");
openIdConnectionOptions.Scope.Add("roles");

return openIdConnectionOptions;
}

我正在我的 GetOpenIdConnectOptions 中设置 ClientId(或者我认为是这样),所以我不清楚错误指的是什么 ClientId。在此处输入代码

编辑:应用设置.json

"OpenIdSettings": {
"Authority": "https://myopenidauthenticationendpointurl",
"ClientId": "myappname",
"CookiePath": "mypath"
}

最佳答案

.AddOpenIdConnect(options => GetOpenIdConnectOptions());

您的 GetOpenIdConnectOptions() 助手返回一个新的 OpenIdConnectOptions 实例,而不是更新由 options = > ... 委托(delegate)。

修复您的方法以获取现有的 OpenIdConnectOptions 值,它应该可以工作:

services.AddAuthentication()
.AddOpenIdConnect(options => SetOpenIdConnectOptions(options));

private void SetOpenIdConnectOptions(OpenIdConnectOptions options)
{
options.ClientId = Configuration["OpenIdSettings:ClientId"];
options.ClientSecret = Configuration["OpenIdSettings:ClientSecret"];
options.Authority = Configuration["OpenIdSettings:Authority"];
options.MetadataAddress = $"{Configuration["OpenIdSettings:Authority"]}/.well-known/openid-configuration";
options.GetClaimsFromUserInfoEndpoint = true;
options.SignInScheme = "Cookies";
options.ResponseType = OpenIdConnectResponseType.IdToken;

options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
// This sets the value of User.Identity.Name to users AD username
NameClaimType = IdentityClaimTypes.WindowsAccountName,
RoleClaimType = IdentityClaimTypes.Role,
AuthenticationType = "Cookies",
ValidateIssuer = false
};

// Scopes needed by application
options.Scope.Add("openid");
options.Scope.Add("profile");
options.Scope.Add("roles");
}

关于c# - ASP.NET Core 2.0 - 必须提供 ArgumentException : Options. ClientId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45742034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com