Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan Evans 629 posts 1016 karma points
    Aug 22, 2011 @ 11:09
    Dan Evans
    0

    Member.BeforeAddToCache Event Handler

    Can someone advise when this event is fired? I assumed it would be each time a member logs in? But it only seems to fire the first time a member logs in and then never again. I'm trying to update a property "previous_login" so I can determine when the user last logged in (as the last login date gets updated to the current date).

     private void init()

           {

              // Member.RemoveMemberFromCache + = new Member.

               Member.BeforeAddToCache += new Member.AddingToCacheEventHandler(Member_BeforeAddToCache);

               Log.Add(LogTypes.Debug, 0, "Member event handlers Member.AddingToCacheEventHandler registered");

           }

     

           void Member_BeforeAddToCache(Member sender, umbraco.cms.businesslogic.AddToCacheEventArgs e)

           {

               Log.Add(LogTypes.Debug, 0, "Member_BeforeAddToCache fired added member " + sender.Id);

               sender.getProperty("previous_login").Value = sender.getProperty("last_login").Value;

               sender.Save();

               sender.XmlGenerate(new System.Xml.XmlDocument());

     

           }

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 22, 2011 @ 11:56
    Dirk De Grave
    0

    Dan,

    membership should handle this for you if you set the correct attributes on the membership provider. Altho I've seen posts about people reporting it doesn't work, in which case it would be great to add this to Codeplex.

    http://msdn.microsoft.com/en-us/library/system.web.security.membershipuser.lastlogindate.aspx and http://our.umbraco.org/wiki/how-tos/membership-providers/umbracomembershipprovider-properties

    Anyway, you could still write your own if you wire up to the LoggedIn event on the <asp:Login> control (Assuming you're using this), and update the member's data or write a record to custom db tables (depending on your scenario)

     

    Cheers,

    /Dirk

  • Dan Evans 629 posts 1016 karma points
    Aug 22, 2011 @ 13:56
    Dan Evans
    0

    Membership handles the last login date.

    The problem seems to be there is a discrepency between what Umbraco classes as logged out and what Membership classes as logged out.

    So the user is presented with a Membership login however they are still in the Umbraco cache.  This can be solved by calling the below. This causes lots of problems across the site if these are not called and the user explicitly logged out.

    Member.RemoveMemberFromCache(m);
               
    Member.ClearMemberFromClient(m);
    I've solved my problem by running the code from the login control rather than as an event handler.

    protected void OnLoggingIn(object sender, System.Web.UI.WebControls.LoginCancelEventArgs e)

        {

            Member m = Member.GetMemberFromLoginName(Login1.UserName);

            string previous_login="";

            if (m != null)

            {

                 previous_login = m.getProperty("last_login").Value.ToString();

            }

            if (Membership.ValidateUser(Login1.UserName,Login1.Password))

            {

                if (m != null)

                {

                    m.getProperty("previous_login").Value = previous_login;

                    m.Save();

                    m.XmlGenerate(new System.Xml.XmlDocument());

                }

            }

        }

Please Sign in or register to post replies

Write your reply to:

Draft