Few months ago, I started working on one of my client’s project and I built it with .NET core 2.1 + Angular 7 and MongoDb.

I can proudly say that development has gone too far and everything is  still stable and secure at this point so thats a win for everyone but  recently I noticed an error on server that I’ve never seen before.

The Problem

I have implemented Jwt token in this project to keep the track of  currently logged-in user and to secure the APIs. There are other  advanced level securities I have applied as well but lets stay on the  track.

The way Jwt should work is all the authorized APIs should validate  the supplied token and if its not a valid one, API must not respond.

Solution

This all used to work fine when we were on our local environment but  after deploying it to the server, User object that is inherited from  ControllerBase class is always null. I tried many solutions but none of  them worked. Here are the few solutions I tried.

  • Set anonymous authentication to false and window authentication  to true. (This was wrong because after this change, Web site started  asking for windows credentials while the portal has its own login  functionality.)
  • Changed Managed Pipeline Mode from “Integrated” to “Classic”. (Didn’t work because it needs to be “Integrated” for .NET Core.)
  • Even  enabled authentication cookie which was not at all required as the  project already have a working (its working only on local environment at  this point) Jwt token authentication functionality.
  • And there were bunch of other solutions suggestion such as modifying webconfig file and what not….!

None of the above solutions worked for me because I had one thing in  my mind that everything works fine on the local environment and it is  not working only after deployment so it has got to do something with the  server setup.

After testing it on a new server and playing with the configuration, I  found out that I had installed a wrong .NET Core runtime on the server.  The project was built with .NET Core 2.1.1 (Runtime 2.1.1 and SDK  2.1.301) and I had v2.1.5 installed on it.

Long story short, Installing .NET Core 2.1.1 on the server with the dumbest look on my face fixed it. 😜

Conclusion

Though the issue was caused by overlooking a tiny information which  was a very important one, it is recommended to have the same  configuration and dependencies as the project is built on to be  installed on the server. Hopefully I’ll be more careful next time.

Thanks for reading, Have a good one! 🖖