ASP.NET 核心 2.0 - ArgumentException:必须提供 Options.ClientId
ASP.NET Core 2.0 - ArgumentException: Options.ClientId must be provided
我有一个 .NET Core 1.1 应用程序,我希望升级到 .NET Core 2.0。更新目标框架和所有依赖项后,我发现我的身份验证设置无法编译。我已更新以说明已删除的属性和 deprecated/moved 方法调用。省略号用于表示为简洁起见而省略的代码。
我现在每次启动我的应用程序时都会收到以下错误
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。enter code here
编辑:
appsettings.json
"OpenIdSettings": {
"Authority": "https://myopenidauthenticationendpointurl",
"ClientId": "myappname",
"CookiePath": "mypath"
}
.AddOpenIdConnect(options => GetOpenIdConnectOptions());
您的 GetOpenIdConnectOptions()
助手 returns 一个新的 OpenIdConnectOptions
实例,而不是更新 options => ...
委托为您准备的 options
对象。
修复您的方法以获取现有的 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");
}
我有一个 .NET Core 1.1 应用程序,我希望升级到 .NET Core 2.0。更新目标框架和所有依赖项后,我发现我的身份验证设置无法编译。我已更新以说明已删除的属性和 deprecated/moved 方法调用。省略号用于表示为简洁起见而省略的代码。
我现在每次启动我的应用程序时都会收到以下错误
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。enter code here
编辑: appsettings.json
"OpenIdSettings": {
"Authority": "https://myopenidauthenticationendpointurl",
"ClientId": "myappname",
"CookiePath": "mypath"
}
.AddOpenIdConnect(options => GetOpenIdConnectOptions());
您的 GetOpenIdConnectOptions()
助手 returns 一个新的 OpenIdConnectOptions
实例,而不是更新 options => ...
委托为您准备的 options
对象。
修复您的方法以获取现有的 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");
}