What is a good way to expand .Net membership to track user logins

As far as I can tell, the main implementation of .Net lastuserlogin membership entries, but does not track every login, I need to do something now. I did some research, and I'm sure one approach is to create a custom class that extends an existing provider with this function and adds an event table to the schema to record each entry.

I saw several articles about this - the closest, probably this is one from Scott Mitchell http://dotnetslackers.com/articles/aspnet/Tracking-User-Activity.aspx , but it's from November 2008 and I'm curious if would someone point me to the best solution for this or confirm that this is still the β€œbest” way to handle this. I would suggest that this is a fairly common requirement. I still click on .Net (using C #), so any information would be appreciated.

EDIT

Progress, but not yet complete. I added the following code to create a new entry in the login activity table:

if (Request.IsAuthenticated) { MembershipUser currentUser = Membership.GetUser(false); string App1 = Membership.Provider.ApplicationName; string username = currentUser.UserName; object guid1 = currentUser.ProviderUserKey; aspnet_CustomActivity login = new aspnet_CustomActivity { UserId = (Guid) guid1, ActivityID = 1, //login DateTime = dt1, ApplicationName = App1 }; db.aspnet_CustomActivities.InsertOnSubmit(login); db.SubmitChanges(); } 

So, I have a little problem when I noticed that ApplicationName is empty, and when I tried to change it in the web.config file, it caused an error loading currentUser.username, but I think I can do it.

What I need to do now to finish is link this to the login page, and I think I can do it based on the tips below. I will send again when finished to complete the answer.

+1
source share
2 answers

We simply created a class that extends SqlMembershipProvider , overriding the ValidateUser method to execute the necessary additional logic. (You just need to change the <membership> section of your web.config file to use this new membership provider).

Edit

There really aren't many. You create a table in your schema that references the Guid user ID in standard membership tables. You create a class to control the interaction with this table. You create your membership provider:

 public class MyMembershipProvider : SqlMembershipProvider { public override bool ValidateUser(string username, string password) { bool validated = base.ValidateUser(username, password); if (validated) { MembershipUser user = GetUser(username, false); // built in to SqlMembershipProvider new LoginTracker().RegisterLogin((Guid)user.ProviderUserKey); } return validated; } } 

Then you register this provider in web.config:

 <membership defaultProvider="MyMembershipProvider" userIsOnlineTimeWindow="60"> <providers> <clear/> <add name="MyMembershipProvider" ... type="MyAssembly.MyNamespace.MyMembershipProvider, MyAssembly"/> </providers> </membership> 

Simple!

+2
source

You will need to implement a custom member provider. Take a look at this: http://msdn.microsoft.com/en-us/library/f1kyba5e.aspx

0
source

Source: https://habr.com/ru/post/1345376/


All Articles