Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Membership provider in Umbraco

I'm trying to create a custom membership provider to authenticate users in Umbraco to an already existing database outside Umbraco. From what I've learned so far it should not be much more work than create a class that inherits from umbraco.providers.members.UmbracoMembershipProvider and overrides the ValidateUser() function.

What I'm wondering is how I'm supposed to do when I wish to include this class to my Umbraco project other than specify it in the web.config file? Is it possible to extend Umbraco in this way whitout re-compile the source code?

EDIT - I wrote an article about how I implemented this on http://marcus-abrahamsson.se/post/Membership-Provider-in-Umbraco

like image 718
Abris Avatar asked Nov 21 '12 11:11

Abris


1 Answers

You can create and build your own module and then copy the .dll into the bin folder of the Umbraco install. No need to recompile Umbraco itself.

I had a very similar issue, but perhaps with a smaller requirement. I was trying to implement the "last logged in" functionality as described in an Umbraco TV video, to display the last logged in date of members. As the code base has changed, the described method was no longer valid. I found there were 2 general ways to get it to work:

Create your own membership provider. Inherit from UmbracoMembershipProvider and update web config to use your provider. I overrode one method, and my provider code looked like:

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 is fired when the user tries to get to secured content.

Note that you now have to update the web.config to point to your custom membership provider.

While this worked, I didn't like moving off of the default provider, editing web.config, etc. and wanted to use the more "standard" approach of the previous version. So I used method 2:

Wire into ApplicationStartupHandler, and subscribe to 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;
        }
   }
}

If I recall, member.beforesave is not something that's fired when a user is created and saved to the membership store; it's actually fired when a user logs in.

like image 160
Jonathan Avatar answered Oct 06 '22 17:10

Jonathan