您可以创建和构建自己的模块,然后将 .dll 复制到 Umbraco 安装的 bin 文件夹中。无需重新编译 Umbraco 本身。
我有一个非常相似的问题,但可能要求较小。我试图实现 Umbraco TV 视频中描述的“最后登录”功能,以显示成员的最后登录日期。由于代码库已更改,所描述的方法不再有效。我发现有两种通用方法可以让它工作:
创建您自己的会员提供商。从 UmbracoMembershipProvider 继承并更新 Web 配置以使用您的提供程序。我覆盖了一种方法,我的提供程序代码如下所示:
using umbraco.BusinessLogic;
using System.Web.Profile;
using System;
namespace zo.Umb.LastLogin
{
// this approac works, and it may be necessary to extend the membership provider in the future, so that's why I'm
// leaving it here. But for now I'm using the ApplicationStartupHandler event subscription method
// in MemberEvent.cs
/// <summary>
/// Inherit the default membership provider and substitute my own method that's fired when a member tries
/// to log in. Note that you must also replace the UmbracoMembershipProvider reference in the web.config
/// with a reference to this one. eg:
/// <add name="UmbracoMembershipProvider" type="zo.Umb.LastLogin.MyMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" />
///
/// also note that, to have custom profile properties appear, they must also be added in the web.config
/// like so:
/// <profile defaultProvider="UmbracoMemberProfileProvider" enabled="true">
/// <providers>
/// <clear />
/// <add name="UmbracoMemberProfileProvider" type="umbraco.providers.members.UmbracoProfileProvider, umbraco.providers" />
/// </providers>
/// <properties>
/// <clear />
/// <add name="lastLogin" allowAnonymous ="false" provider="UmbracoMemberProfileProvider" type="System.DateTime" />
/// </properties>
///</profile>
/// </summary>
public class MyMembershipProvider : umbraco.providers.members.UmbracoMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
var success = base.ValidateUser(username, password);
if (success)
{
var user = GetUser(username, true);
var profile = ProfileBase.Create(user.UserName);
profile["lastLogin"] = DateTime.Now;
profile.Save();
}
return success;
}
}
}
当用户尝试访问受保护的内容时会触发 ValidateUser。
请注意,您现在必须更新 web.config 以指向您的自定义成员资格提供程序。
虽然这行得通,但我不喜欢离开默认提供程序、编辑 web.config 等,并希望使用以前版本的更“标准”的方法。所以我使用了方法2:
连接到 ApplicationStartupHandler,并订阅 Member.BeforeSave
using System;
//using umbraco.BusinessLogic;
using umbraco.businesslogic;
using umbraco.cms.businesslogic.member;
namespace zo.Umb.LastLogin
{
public class MemberEvent : ApplicationStartupHandler
{
public MemberEvent()
{
Member.BeforeSave += new Member.SaveEventHandler(Member_BeforeSave);
}
void Member_BeforeSave(Member sender, umbraco.cms.businesslogic.SaveEventArgs e)
{
//Log.Add(LogTypes.Debug, sender.Id, "Member_AfterAddToCache");
sender.getProperty("lastLogin").Value = DateTime.Now;
}
}
}
如果我记得, member.beforesave不是在创建用户并将其保存到会员商店时触发的东西。它实际上是在用户登录时触发的。