Category: recordVisitors

RecordVisitors–code–part 2

Now , after the idea is complete, let’s see the code. ( Yes, I know that TDD will be better … )

For the implementation :

1. Problems with the manual test-  how to create a Fake User – see below the solution.

2. Problems of how to let user define his own data

You can find the original code at https://github.com/ignatandrei/RecordVisitors/releases/tag/just-Code

I do not want to bother with details about implementation – so some statistics ( obtained from VS with Analyze => Calculate Code Metrics)

Those are the classes  / Interfaces:

  1. Extensions – used to easy registration in Startup
  2. IRecordVisitorFunctions – to let the user define his function to get the name of the user
  3. IUsersRepository – to let the user define his function to save / retrieve from database
  4. RecordVisitorFunctions – implementation
  5. RecordVisitorsMiddleware
  6. UserRecorded – what to save in the default implementation
  7. UserRecordVisitors –  the default DBContext to save
  8. UsersRepository – default database implementation with EF in memory

Created also an ASP.NET Core WebAPI project to test . The following was added:

1. A MockAuthenticatedUser (  credits to https://visualstudiomagazine.com/Blogs/Tool-Tracker/2019/11/mocking-authenticated-users.aspx )

2.  To the startup:

services.AddAuthentication(“BasicAuthentication”)
     .AddScheme<AuthenticationSchemeOptions,
                   MockAuthenticatedUser>(“BasicAuthentication”, null);

app.UseAuthentication();
//put AFTER authentication
app.UseRecordVisitors();

app.UseEndpoints(endpoints =>
{
     endpoints.MapControllers();
     endpoints.UseVisitors();
});

The number of lines are aproximatively 200 ( the total number of lines written by VS is 1000 )

RecordVisitors-idea–part1

It will be interesting to have a middleware of .NET Core that can record the name of the visitors in order to see what are the latest visitors of your site .

What will be the uses ?

  1. See when the people has visiting your site
  2. See the latest visitors
  3. If you have a site for mobile and a site for web , prevent multiple logins
  4. Fun 😉

What characteristics should have ?

  1. Record visitors
  2. Make an endpoint – recordVisitors/AllVisitors5Min to see the latest visitors as JSON
  3. Let the programmer choose how to obtain  the name of the visitor  ( via Forms, , AD Identity, JWT,  Azure , OAUTH…)
    1. Choose a default to work fast
  4. Let the programmer choose his variant of persisting ( SqlServer, Mongo , Sqlite, CSV… ) where to be recorded
    1. Choose a default  to work fast
    2. Make as an event to intercept and  add additional details

Now let’s see what is for the programming!

Andrei Ignat weekly software news(mostly .NET)

* indicates required

Please select all the ways you would like to hear from me:

You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.