Umbraco Get Current Member or Member By ID

It seems like most pages when you Google for getting the current member or a member by id in Umbraco seem to be from back in 2010 or 2012, well things have changed since the businesslogic days and we're now in the world of Umbraco Services, how do we use them? Well, it's simpler than you think.

It used to be that you'd have to use the built in security methods such as Membership.GetUser(), or umbraco.cms.businesslogic.member.Member.GetCurrentMember(), but the former doesn't provide an Umbraco model and the latter, is horribly long, not very memorable and is also Obsolete!

Introducing the Membership Service:

You can access the membership service pretty easily by calling Services.Membership service and letting intellisense do the rest of the work for you by letting you know what else it offers, a quick example for getting e member by his/her username would be:

Services.MemberService.GetByUsername("username")

But that only works within a view/controller because they have an Umbraco context built in. For when you're not in a view or controller and you still need to access the services, it's a simple call to get the application context and suddenly they're all available to you:

ApplicationContext.Current.Services.MemberService.GetByUsername("username")

A great example for why you might use the above is for you're writing a helper method/class that makes use of the membership service but you don't want to take it as a parameter.

Getting the current member:

To get the current member, you'll need to use a mixture of the membership service and the .NET Membership provider, but it's simple enough, just call it like so:

ApplicationContext.Current.Services.MemberService.GetByUsername(Membership.GetUser().UserName);

Tadahh!

P.S: Just for the sake of a complete article, ApplicationContext is located in Umbraco.Core, so go ahead and add a using statement for that before calling it. Also Membership is located in System.Web.Security.


Published at

Tags: Umbraco,C#,MVC

Luke Alderton

Comments

Lee Messenger
Just remember you are hitting the database with the call from the MemberService (Unless things have changed). You should (Again unless things have changed) still be able to use MembershipHelper to get a cached IPublishedContent version of the member. See this post I wrote a while back. https://24days.in/umbraco-cms/2014/dealing-with-members/
04/09/2018
Share with
Tags
Latest Comments
By Mark Gentry on Windows Server 2019 - Change product key does nothing
20 Aug 2021, 03:30 AM
By Cathy on In-Place Upgrade for a Windows Server domain controller
31 Jul 2021, 18:28 PM
By Mr. Greymatter on Raspberry Pi - Running Java app on Raspbian
16 Feb 2021, 07:35 AM
By Mikko Seittenranta on Xamarin Forms multiple instances of same app open
16 Feb 2021, 04:34 AM
By Andrew on Auto/Custom height on Xamarin Forms WebView for Android and iOS
22 Jan 2021, 22:15 PM
By Nick on Raspberry Pi - Running Java app on Raspbian
14 Oct 2020, 19:37 PM
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development