Evildommer5 Evildommer5 - 1 year ago 249
ASP.NET (C#) Question

Asp.net Core 2.0 basic JWT missing ApplicationCookie

I am currently upgrading my Web API project to .Net Core 2.0

and it seem AddIdentity.Cookies is no longer available?.

can anyone point me in the right direction

Thanks!

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


enter image description here

Answer Source

Based on the code on GitHub, it seems to me that you should use ConfigureApplicationCookie:

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

The extension method on GitHub: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity/IdentityServiceCollectionExtensions.cs#L39

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download