2017-08-17 47 views
4

我有一个.NET Core 1.1应用程序,我希望升级到.NET Core 2.0。更新目标框架和所有依赖关系后,我发现我的身份验证设置无法编译。我已经更新以解释已删除的属性和已弃用/已移动的方法调用。为了简洁,省略用于表示代码。ASP.NET Core 2.0 - ArgumentException:必须提供Options.ClientId

我现在收到以下错误,每当我开始我的应用程序enter image description here

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

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方法。这里是我的核心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; 
    } 

我设置的客户端Id(或因此我想)我GetOpenIdConnectOptions所以我什么ClientID的错误指的是目前还不清楚新的代码。 enter code here

编辑: appsettings.json

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

是否'配置[ “OpenIdSettings:客户端Id”]'有一个值? –

+0

用appsetting.json的'OpenIdSettings'部分添加了原始帖子的编辑 – tralmix

回答

3

.AddOpenIdConnect(选项=> GetOpenIdConnectOptions());

你助手返回更新由options => ...委托为您准备的options对象的新OpenIdConnectOptions实例,而不是。

修复你的方法,以利用现有的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"); 
} 
+1

谢谢。认证再次完美。 – tralmix

相关问题