ASP.NET Core 2.0 - ArgumentException: .ClientId Parameters Must Be Provided

I have a .NET Core 1.1 application that I want to upgrade to .NET Core 2.0. After updating the target structure and all the dependencies, I found that my authentication setting would not compile. I updated to account for remote properties and obsolete / moved method calls. The ellipses used to represent the code are omitted for brevity.

Now I get the following error when starting the application enter image description here

1.1 Code - Inside public void Configure()the Startup.cs Method

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);

Everything I read shows that this process has moved on to the method ConfigureServices. Here is my new code for 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;
    }

I set ClientId (or so I thought) in mine GetOpenIdConnectOptions, so it is not clear what ClientId refers to.enter code here

: appsettings.json

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

.AddOpenIdConnect(options = > GetOpenIdConnectOptions());

GetOpenIdConnectOptions() OpenIdConnectOptions options, options => ....

, OpenIdConnectOptions, :

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 => 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");
}
+4

Source: https://habr.com/ru/post/1683920/


All Articles