简体   繁体   中英

setting asp.net CORE 2 authentication cookie while using bearer token authentication

I am using IdentityServer and JWT bearer tokens to authenticate requests in an angular 4 / ASP.NET Core 2 web application.

The authentication all works, and I can obtain the bearer token. Unfortunately the project has a few pages (like the hangfire dashboard) that require a regular forms authenticated cookie.

How can I programmatically create a cookie, and set it in the angular application so that when I navigate to the legacy pages, the cookie is present, and the user can be authenticated?

The regular login page is still present, and I can login using forms authentication as well, but I would rather just login using the bearer token, and then obtain the authentication cookie as if I had logged in using forms as well.

The bearer token will ensure that an unauthenticated user cannot obtain the cookie, that part is already taken care of. I just need to know how to create the cookie and return it.

You can have both Cookies and JWT authentication at the same time in one project. first add both cookie and JWT authentication in ConfigureServices method:

services.AddAuthentication()
  .AddCookie(options => options.SlidingExpiration = true)
  .AddJwtBearer(options =>
  {
    // JWT setup
  });

in Configure method just add app.UseAuthentication();

and put

[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]

on top of controllers where you want change authentication scheme to JWT.

for more info read Two AuthorizationSchemes in ASP.NET Core 2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM