Working on a site with member logins I'd like to track last user login. So I bought access to umbraco.tv to see how that could be done (did also check out examples here at our.umbraco.
So I have the following code, which is used in v4.0.3 site:
using System;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic;
using UmbracoMember = umbraco.cms.businesslogic.member.Member;
namespace TI.Backend.Umbraco
{
public class MemberEvents : ApplicationBase
{
public MemberEvents()
{
UmbracoMember.AfterAddToCache += new UmbracoMember.AddingToCacheEventHandler(UmbracoMember_AfterAddToCache);
}
void UmbracoMember_AfterAddToCache(UmbracoMember sender, AddToCacheEventArgs e)
{
Log.Add(LogTypes.Debug, -1, "User login: " + sender.getProperty("firstname").Value.ToString());
sender.getProperty("lastLogin").Value = DateTime.Now;
sender.Save();
}
}
}
Which is placed in a project that is compiled into a dll and copied to the website /bin folder. But the code doesn't run when using the login page. There is no record in the log table, and the user lastLogin value isn't set after login.
Can anybody see anything wrong with this code?
Is it possible to run a member login without the AfterAddToCache event happening?
Well, maybe the AddToCache method is failing or being cancelled by another event handler.
Check out the trace for the page (add ?umbDebugShowTrace=true to the url) and find: "Member added to cache" to see if this works properly. The AfterAddToCache event happens very soon after that:
// Debug information
HttpContext.Current.Trace.Write("member",
string.Format("Member added to cache: {0}/{1} ({2})",
m.Text, m.LoginName, m.Id));
_memberCache["umbracoMembers"] = umbracoMembers;
FormsAuthentication.SetAuthCookie(m.LoginName, false);
m.FireAfterAddToCache(e);
In any case, check the windows event log to see if you're getting any exceptions. Your code looks valid enough.
After digging into this some more, I noticed that the functionality is actually configurable. You can create an attribute on the umbracoMemberShipProvider section in web.config, that points to the alias you want to store last login date in like this:
In order for the New event to work, i had to add the defaultMemberTypeAlias attribute to the membership provider tag. Only then would the New event fire in the class.
In order to capture the lastLogin time stamp, i had to use the umbracoLastLoginPropertyTypeAlias. The "Member.AfterAddToCache" event doesn't seem to fire for me at all. I haven't tested any of the other events.
Why doesn't my AfterAddToCache event handler run?
Working on a site with member logins I'd like to track last user login. So I bought access to umbraco.tv to see how that could be done (did also check out examples here at our.umbraco.
So I have the following code, which is used in v4.0.3 site:
Which is placed in a project that is compiled into a dll and copied to the website /bin folder. But the code doesn't run when using the login page. There is no record in the log table, and the user lastLogin value isn't set after login.
Can anybody see anything wrong with this code?
Is it possible to run a member login without the AfterAddToCache event happening?
Any ideas?
Regards
Jesper Hauge
Well, maybe the AddToCache method is failing or being cancelled by another event handler.
Check out the trace for the page (add ?umbDebugShowTrace=true to the url) and find: "Member added to cache" to see if this works properly. The AfterAddToCache event happens very soon after that:
In any case, check the windows event log to see if you're getting any exceptions. Your code looks valid enough.
Thanks for your ideas Sebastiaan.
After digging into this some more, I noticed that the functionality is actually configurable. You can create an attribute on the umbracoMemberShipProvider section in web.config, that points to the alias you want to store last login date in like this:
This in effect removes the need for the code on the top.
Regards
Jesper Hauge
Hi all,
I have the same problem when I login. I use the code from umbraco.tv but when I login with a member nothing happens / the code isn't executed.
Also I can't find "Member added to cache" in my trace. The begin of my trace looks like this
Somebody any ideas?
Jeffrey
I just ran into this problem with my new install.
In order for the New event to work, i had to add the defaultMemberTypeAlias attribute to the membership provider tag. Only then would the New event fire in the class.
In order to capture the lastLogin time stamp, i had to use the umbracoLastLoginPropertyTypeAlias. The "Member.AfterAddToCache" event doesn't seem to fire for me at all. I haven't tested any of the other events.
I'm using v. 4.0.3...
I'll check to see if theres an issue about this in codeplex, or add one.
.Jesper Hauge
is working on a reply...