Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net WebApi Core 2.0 Identity With JWTBearer Without cookies

I'm building Web service using ASP.Net Core WebApi. I would like to use Identity and also JWTBearer authentication. My code in ConfigureService method of Startup class id:

services.AddIdentity<User, Role>()
      .AddDefaultTokenProviders();

  //JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

  services.AddAuthentication(options =>
  {
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  })
  .AddJwtBearer(options =>
  {
    options.RequireHttpsMetadata = false;
    options.SaveToken = true;
    options.Audience = Configuration.GetSection("JwtIssuer").Value;
    options.ClaimsIssuer = Configuration.GetSection("JwtIssuer").Value;
    options.TokenValidationParameters = new TokenValidationParameters
    {
      ValidateIssuer = true,
      ValidateAudience = true,
      ValidateLifetime = true,
      ValidateIssuerSigningKey = true,
      ValidIssuer = Configuration["JwtIssuer"],
      ValidAudience = Configuration["JwtIssuer"],
      IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"]))
    };
  });

Configure method of Startup class contains:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UseMvc();

My problem is that when I call signInManager.PasswordSignInAsync it include cookie with authentication token to response. But i Would like only to use JwtBearer in header. Is there any option how to disable cookies for SignInManager?

like image 208
PeterMacko Avatar asked Feb 16 '18 15:02

PeterMacko


1 Answers

Use SignInManager.CheckPasswordSignInAsync(): it does exactly the same checks but unlike SignInManager.PasswordSignInAsync, doesn't return any cookie.

like image 101
Kévin Chalet Avatar answered Sep 22 '22 13:09

Kévin Chalet