Last Login Date

Katrina Miday asked on May 13, 2014 09:46

I'm trying to get the user's last login date. I know that this value is over-written when the user logs in so I'm trying to catch it before it is updated during the login process.

I found this forum post: http://devnet.kentico.com/forums?forumid=68&threadid=34320

But I'm stuck as to where I would put the event handler. Would I create a custom login form? A basic webpart?

Also in the question it sounds like Froggeye ended up saving the value to a table - I just need it for the user session.

Any information is appreciated. Thanks in advance.

Correct Answer

Brenden Kehren answered on May 13, 2014 11:20

What I did was added a new field to the User Settings and saved it off there vs. session. Made for a more robust solution. The event handler documentation for v7 can be found here.

Here is my code sample in the ClassLoaderModule.cs file:

[MyCustomClassLoaderModule]
public partial class CMSModuleLoader
{

/// <summary>
/// Summary description for MyCustomClassLoaderModuleAttribute 
/// </summary>
public class MyCustomClassLoaderModuleAttribute : CMSLoaderAttribute
{
    private UserInfo lastUserInfo;

    /// <summary>
    /// Initializes the module
    /// </summary>
    public override void Init()
    {
        SecurityEvents.Authenticate.Execute += Authenticate_Execute;
    }

    /// <summary>
    /// On authenticate, update the users last login date info
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Authenticate_Execute(object sender, AuthenticationEventArgs e)
    {
        if (e.User != null)
        {
            UserInfo ui = e.User;
            UserSettingsInfo usi = UserSettingsInfoProvider.GetUserSettingsInfoByUser(ui.UserID);
            if (ui.LastLogon != DateTime.MinValue)
            {
                usi.SetValue("UserLastAccountActivity", ui.LastLogon);
                UserSettingsInfoProvider.SetUserSettingsInfo(usi);
            }
        }
}
}
}
0 votesVote for this answer Unmark Correct answer

Recent Answers


Katrina Miday answered on July 29, 2014 18:34

Thanks Brenden! It's been a while since I looked at this, but finally had some time to try getting it to work again. Worked like a charm.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.