TGIMBA .NET Core API – The New API and Service Layers – Part 1 – Miscellaneous Methods

Git  Code Commit (all commits between last post and this commit (link))

Welcome back!

Next for The Globe In My BucketList Application (TGIMBA) Application Programmable Interface (API) are the API and service layers.  The biggest change is the API is in its own solution now.

1

Like the data layer, I see no reason to change the service contract, just the implementation underneath.  So I added the suffix ‘_Old’ to the current service interface and added a new one with a stub implementation.  The Misc methods are implemented with tests.

1

Highlights:

  • GeneratorHelper.cs – I added a method for generating a JSON Web Token (JWT) based on a previous post here.

2

  • GeneratorHelper.cs – The private key and issuer methods just return strings.  I generated the private key using existing .NET classic ‘RSACryptoServiceProvider’ class.  Apparently, it does not exist in .NET Core 2.x, but does in .NET Core 3.x.  I am running .NET Core 2.2 now.  Eventually I will upgrade and remove the .NET Class project 🙂

2

  • I updated the method for hashing my passwords.  Specifically, using the ‘Rfc2898DeriveBytes’ provider in conjunction with the HashAlgorithmName.SHA256 algorithm.  Previously, I had just used the SHA256 algorithm.  Apparently, this is considered more secure.   References #4 and #6 were especially helpful with this.

1

  • I had planned on doing the service layer first, but found it difficult to break the parameter validation and error handling between the API and server layers.  So, these two functions are handled in the API layer since it is the entry and exit points. Also, based on what I have read, specific errors (i.e. 404, 500, etc) are easier when they reside in the API layer.
    • Interesting side bit.  As I was looking for a general error to return in case of an issue (remember, you never want to return specifics but a general error with a code and log the specifics in your database), I discovered that the ASP .NET Core team has removed ‘HttpResponseException’.  The details are in reference #2, but their justification seems a bit flat to me.  As developer, I want options and not to be forced down a certain route.  I also found this in my research (see blog post here) on ways to do .NET Core authorization and authentication.  They had removed specific interfaces/implementations others had used.  It would be nice if options are persisted and not removed just because a designer on a team feels all folks should use a specific approach.  More to the point, in reference #2, there are a couple of folks who are implementing their own replacement for HttpResponseException.    I ended up placing a try catch in each API method and in case of an error, I return a 500 via the HttpStatusCode enumeration.

The API/service layer will be done in three posts – misc, user and bucket list related methods/tests.  I would have done all of this in one blog post, but it would be another 2-3 months before it was ready.

API Layer1

Service Layer1

I am trying to work in digestible chunks that fit in with working full time and having a family 🙂

Stay tuned!

References

  1. https://stackoverflow.com/questions/10732644/best-practice-to-return-errors-in-asp-net-web-api
  2. https://github.com/aspnet/Mvc/issues/4311
  3. https://stackoverflow.com/questions/37793418/how-to-return-http-500-from-asp-net-core-rc2-web-api
  4. https://stackoverflow.com/questions/38794670/how-to-create-encrypted-jwt-in-c-sharp-using-rs256-with-rsa-private-key
  5. https://stackoverflow.com/questions/1307204/how-to-generate-unique-public-and-private-key-via-rsa
  6. https://stackoverflow.com/questions/18648084/rfc2898-pbkdf2-with-sha256-as-digest-in-c-sharp
  7. https://stackoverflow.com/questions/11271057/how-to-use-moq-to-verify-that-a-similar-object-was-passed-in-as-argument

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s