2017-11-12 15 views
0

私は以下のコードを1.1で動作させました。.netコア1.1から2.0へのIDのアップグレード

public static IServiceCollection RegisterRepositoryServices(this IServiceCollection services) 
     { 
      services.AddIdentity<ApplicationUser, IdentityRole<int>>(
       config => { config.User.RequireUniqueEmail = true; 
        config.Cookies.ApplicationCookie.LoginPath = "/Account/Login"; 
        config.Cookies.ApplicationCookie.AuthenticationScheme = "Cookie"; 
        config.Cookies.ApplicationCookie.AutomaticAuthenticate = false; 
        config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() 
        { 
         OnRedirectToLogin = async ctx => 
         { 
          if (ctx.Request.Path.StartsWithSegments("/visualjobs") && ctx.Response.StatusCode == 200) 
          { 
           ctx.Response.StatusCode = 401; 
          } 
          else 
          { 
           ctx.Response.Redirect(ctx.RedirectUri); 
          } 
          await Task.Yield(); 
         } 
        }; 
       }).AddEntityFrameworkStores<VisualJobsDbContext, int>() 
       .AddDefaultTokenProviders(); 

      services.AddEntityFrameworkSqlServer().AddDbContext<VisualJobsDbContext>(); 

      services.AddScoped<IRecruiterRepository, RecruiterRepository>(); 
      services.AddSingleton<IAccountRepository, AccountRepository>(); 

      return services; 
     } 

それは今config.Cookiesを参照するセクションが好きではありません....

私はネットを検索してきたが、私は本当にこれを置き換えるために何かを見つける傾けます。

+2

_Itは今config.Cookiesに言及している部分が好きではありません.... _は非常に曖昧な問題の記述です。あなたは精緻化できますか?コンパイルエラーが出ますか? –

+0

これ以上の4つの異なるCookie構成はなく、1つだけです。 [GitHubのアナウンス]を読んでください(https://github.com/aspnet/Announcements/issues/262) – Tseng

+0

@MartinLiversageいいえ、それはVS2017で赤線ではコンパイルされません。それは廃止されたと言います。 – bilpor

答えて

0

あなたはクッキー認証サービスを追加するAddIdentityを呼び出してから、次のようにConfigureApplicationCookieを使用して設定を構成する必要があります、あなたのConfigure()方法では、次のように呼び出すことを忘れないでください

services.AddIdentity<ApplicationUser, IdentityRole<int>>(config => { 
     config.User.RequireUniqueEmail = true; 
    }) 
.AddUserStore<UserStore<ApplicationUser, IdentityRole<int>, VisualJobsDbContext, int>>() 
.AddDefaultTokenProviders(); 

services.ConfigureApplicationCookie(o => { 
    o.LoginPath = new PathString("/Account/Login"); 
    o.Events = new CookieAuthenticationEvents() 
    { 
     OnRedirectToLogin = async ctx => 
     { 
      if (ctx.Request.Path.StartsWithSegments("/visualjobs") && ctx.Response.StatusCode == 200) 
      { 
       ctx.Response.StatusCode = 401; 
      } 
      else 
      { 
       ctx.Response.Redirect(ctx.RedirectUri); 
      } 
      await Task.Yield(); 
     } 
    }; 
}); 

また:

app.UseAuthentication(); 

追加読書: Migrating Authentication and Identity to ASP.NET Core 2.0

注:この資料の次のセクションでは、AutomaticAuthenticateオプションに関して :

設定デフォルト認証スキーム1.1で

AuthenticationOptions基底クラスのAutomaticAuthenticateAutomaticChallenge 特性でした単一の認証方式に設定することを意図しています。 これを強制する良い方法はありませんでした。 2.0では、これらの2つのプロパティが個別のAuthenticationOptionsインスタンスの プロパティとして削除されました。彼らは はStartup.csの ConfigureServicesメソッド内AddAuthenticationメソッド呼び出しで設定することができます。

services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);

関連する問題