2017-08-17 18 views
4

.NET Core 2.0にアップグレードしたい.NET Core 1.1アプリケーションがあります。ターゲットフレームワークとすべての依存関係を更新すると、認証設定がコンパイルされないことがわかりました。私は、削除されたプロパティと推奨されない/移動されたメソッド呼び出しを考慮に入れて更新しました。簡略化のため省略されたコードを表すために使用される楕円記号。 Startup.csの内部public void Configure()方法私はショーを読んでいるASP.NET Core 2.0 - ArgumentException:Options.ClientIdを提供する必要があります

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に移動しました -

は、私は今、私は自分のアプリケーションenter image description here

1.1コードを起動するたびに、次のエラーが表示されます方法。ここで私はエラーが参照しているのClientIDかについては不明だ私は私のGetOpenIdConnectOptionsに(またはそう思った)ClientIdを設定しています

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

コア2.0

のための私の新しいコードがあります。 enter code here

編集: appsettings.json

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

'設定[ "OpenIdSettings:ClientIdを"]はない'価値がありますか? –

+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

関連する問題