Problems combining JWT Bearer Authenticating Web API with Asp.Net Core 2.0

Kim Raaness

I have the following setup:

Web API with JWT Bearer Auth

Asp.Net Core 2.0 MVC handling identities and providing views

AngularJS - client

Angular is requesting JWT token and passing it on subsequents http requests. AJAX calls are working fine. The problem is if I request an MVC action with [Authorize] through my browser, that token is obviously not validated, because there is no cookie and no auth header.

How would I go about implementing signin functionality to non-ajax requests?

I assume I need to implement some sort of Cookie Authentication, but I was hoping to avoid it after moving to JWT.

I know this could probably be solved by migrating to SPA, but I was wondering if there was a way to keep todays solution with MVC serving views - old habit :(

twinmind

If you want server-side JWT authentication, then you need to store your JWT in a cookie instead of local storage. Cookies are issued for your site's domain, so when you request youre MVC app for a view and assuming your API and your MVC site have the same origin (same schema + host + port), there should be no problem for protected MVC controller to process incoming cookie in HTTP(S) request and make a decision on how to respond. WebAPI should also be able to handle cookies with JWT payload. I found nice article with sample project about ASP.NET Core MVC Authentication here: https://auth0.com/blog/asp-dot-net-core-authentication-tutorial/ For your AngularJS SPA it shouldn't be a problem to set JWT to a cookie instead of putting it to local storage. It would be actually more secure way to store JWT, but you need to make sure your JWT won't get too long, because cookie payload size is limited. More about it here: https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JWT bearer token Authorization not working asp net core web api

Using Microsoft Graph token to secure ASP.NET Core Web API with Jwt Bearer tokens

ASP.NET Core web api editing Bearer auth

Call .NET CORE Web API Secure End-point from Angular by passing Azure B2C JWT Bearer token

Authorization for JWT bearer in Swashbuckle .NET Core 2

transfer JWT Authentication implementation from .net core 2 to asp.net web api 2

Validating B2C JWT tokens in Asp.Net Core Web Api

Cognito JWT Authorize in ASP.NET Core 6 Web API

ASP.NET Core Web API Facebook JWT Authentication

Check IP with JWT Authorization in ASP.NET Core Web Api

JWT Authentication not working in ASP.NEt core web api

Facebook JWT authentication using ASP.NET Core Web API

Use JWT (Authorization: Bearer) in Swagger in ASP.NET Core

ASP.NET Core JWT Bearer Token Custom Validation

JWT Bearer ASP.Net Core 3.1 User is blank on server

.net core web api jwt

ASP.NET Core - Cache problems with SPA route in Web API

.NET Core API - does the middleware set the JWT bearer token in the header?

static files - asp.net web api core 2.1 authentication scheme Bearer

.NET Core Web Api Azure AD and Swagger not authenticating

Migrate ASP.NET Framework to ASP.NET Core 3.1 with JWT Bearer authentication from ADFS

Asp.net Web API .NET Core 3.1 and Azure AD - system.unauthorizedaccessexception: neither scope or roles claim was found in the bearer token

ASP.NET Core 2.0 combining Cookies and Bearer Authorization for the same endpoint

JWT authentication for ASP.NET Web API

.NET Core Authorization - Constant 403 on JWT Bearer

Asp.Net Core 2 Validating bearer token

How can my ASP.NET Core App Service Web API support both AAD Bearer Token and Client Certificate Auth?

create jwt token in c# asp.net core web api

Skip JWT Auth during Tests ASP.Net Core 3.1 Web Api