ASP.Net Core: Linking a signed in user to a business domain

David Jacobs
May 21, 2019 · 3 min read

Returning to .Net Core from my time learning Node.js application development, meant deciding where to resume. I felt with web development in continuous change I would start updating my skills with ASP.Net Core. I wanted to know what Microsoft is providing out of the box for the .Net Core developer. That means selecting the appropriate template option in Visual Studio to create my project. I like to begin with selecting the empty solution option. For me this like being a given a blank canvas to create my painting. I can decide the structure of the solution from scratch to add projects as and when required.

I decided to create a business scenario for my domain. The scenario I use is a made up food company employing testers to record food quality based upon set criteria. Each food item would be sent to the tester through the post for the tester to log in and record their testing results for each food item provided. The reason for this idea is because of the template I wanted to try out with Visual Studio.

I chose to create the ASP.Net Core solution with Microsoft’s user sign in support included. I didn’t went to reinvent the wheel given a sign in solution is already available to me to use. I have to point out that this point I am using SQL Server for my database. Microsoft’s Identity classes uses SQL Server by default to store the user login and claim principal data. I only reconfigured the sign in setup to use my own database in SQL Server. I am not using Entity Framework. Microsoft has made configuring sign in support easy in ASP.Net Core covering for example, password strength. After building the code I can login and see my user in the database with a hashed password stored. This is a good starting point. I have a username and password sign in system up and running out the box working within minutes with a basic UI to go.

So the question going through my mind is how I do link my signed user to my other database tables I created to store my food testing results? I wanted to know from querying my database which user received packages through the post and entered the results from their tests. ASP.Net Core relies upon middleware configuration to get up and running. You can register services to be injected in your controllers and views (more on this later) to call the methods available. I wanted to register a service to get hold of the signed in user’s data at runtime with ease. An ideal solution is one of reuse across my domain to get the user’s id after sign in to relate to my other database tables. With this I can record user audit and business activity with test results assigned to an authenticated user id.

From research I found the IHttpContextAccessor interface. This is one of those many interfaces with the least available documentation from Microsoft. The link is here:

The following code snippet is the registration code I wrote to register the IHttpContextAccessor. I wrote an extension method extending the IServiceCollection interface. The is good clean approach to registering additional services.

In the ConfigureServices code I simply call:

This provided the solution I needed to create my SignedInUser service to get the id of the signed in user. I can further extend my interface to have methods to get other user claim data if so desired. The code below is my service with the injected context accessor. Again this is registered in the ConfigureService code using an extension method extending the IServiceCollection interface. Now I can call my GetUserId method and return the id of the signed in user through my own sign in service.

I mentioned injecting into the views the services registered. My code base is an MVC app. I’m not using controllers. The views have a class code behind file, so therefore I can create a constructor and inject the service I require.

My learning continues…

Compare the Market

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store