nbarbettini / SimpleTokenProvider

Sample middleware component for ASP.NET that generates JWT access tokens
Apache License 2.0
288 stars 90 forks source link

Why a MiddleWare and not a Controller? #23

Open cwienands1 opened 7 years ago

cwienands1 commented 7 years ago

I am currently integrating your token provider into my own SPA application. I really like it because it is small enough for me to understand and integrate, unlike all those humongous packages like IdentityServer4 ;-) But I am wondering why you chose to implement the TokenProviderMiddleware class as a middleware and not as a controller? After all, all it does is wait for a POST on e.g. /api/token. That task could easily be accomplished with a controller, and the ASPNetCore pipeline would save one Invoke with each request. I'm still new to ASPNetCore so there might be an obvious reason but I didn't see that. Thanks!

nbarbettini commented 7 years ago

Thanks for checking out the code! The reasons I implemented this sample as a middleware are

In your own project, you definitely could implement this endpoint as a controller if you want.

AndrewGalea commented 7 years ago

The problem I am getting using the MiddleWare component is related to CORs. In my controllers I can simply add an [EnableCors("MyPolicy")] attribute. I cannot work out how to do the same when it is a MiddleWare component.

Thoughts?

ps - to provide more background, the call works fine when executed from Postman, however I get the CORs error when trying to make the authentication call from within my Angular application.

AndrewGalea commented 7 years ago

Ok never mind, simply had to enable CORs globally instead of at the Controller level... 👍