Asp.net Core 2.0 基本 JWT 缺少 ApplicationCookie

Asp.net Core 2.0 basic JWT missing ApplicationCookie

我目前正在将我的 Web API 项目升级到 .NET Core 2.0 似乎 AddIdentity.Cookies 不再可用。

谁能给我指出正确的方向?

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(Configuration);

        services.AddMvc();

        // Add framework services.
        services.AddDbContext<ApplicationContext>(
            options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddDbContext<AuthContext>(options => options.UseSqlServer(Configuration.GetConnectionString("AuthConntection")));

        services.AddIdentity<ApplicationUser, MyRole>(cfg =>
        {
            cfg.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents
            {
                OnRedirectToLogin = ctx =>
                {
                    if (ctx.Request.Path.StartsWithSegments("/api"))
                        ctx.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
                    return Task.FromResult(0);
                }
            };
        })
            .AddUserStore<ApplicationUserStore>()
            .AddDefaultTokenProviders()
            .AddEntityFrameworkStores<AuthContext, Guid>();}

根据 GitHub 上的代码,在我看来你应该使用 ConfigureApplicationCookie:

services.AddIdentity<ApplicationUser, MyRole>();
services.ConfigureApplicationCookie(options =>
{
    options.Events = new CookieAuthenticationEvents
    {
    };
});

GitHub上的扩展方法:https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs#L39