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

时间:2017-08-17 17:56:29

标签: c# asp.net openid-connect asp.net-core-2.0

我有一个.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。enter code here

编辑: appsettings.json

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

1 个答案:

答案 0 :(得分:6)

  

.AddOpenIdConnect(options => GetOpenIdConnectOptions());

您的<script> class IconToggle extends Polymer.Element { static get is() { return "icon-toggle"; } constructor() { super(); } } customElements.define(IconToggle.is, IconToggle); </script> 助手会返回新的GetOpenIdConnectOptions()个实例,而不是更新OpenIdConnectOptions代表为您准备的options对象。

修复您的方法以获取现有的options => ...值,它应该有效:

OpenIdConnectOptions