使用ASP.NET成员资格的Umbraco成员资料数据

时间:2012-10-20 20:32:51

标签: c# asp.net-membership umbraco

使用旧的api访问会员资料很容易

var arne = Member.GetMemberFromLoginName("arne"); // deprecated
var arneProperty = arneUmbraco.getProperty("property");

它可以工作,但不推荐使用GetMemberFromLoginName,并且引用留给ASP.NET成员资格。

var arne = Membership.GetUser("arne");

这很公平,但我如何加载物业?在我工作的上下文中,我无法访问HttpContext.Current.Profile,因为我实际上是自己加载用户。

var profile = HttpContext.Current.Profile; // This won't work...

有什么建议吗?感谢

1 个答案:

答案 0 :(得分:0)

我有一个非常类似的问题,但可能要求较小。我试图实现Umbraco电视视频中描述的“上次登录”功能,以显示成员的最后登录日期。随着代码库的更改,所描述的方法不再有效。我发现有两种方法可以让它发挥作用:

创建您自己的会员提供商。继承自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等,并希望使用之前版本的更“标准”方法。所以我用方法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是触发的内容;它实际上是在用户登录时触发的。