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
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By gf on Xamarin forms - Stop/Cancel back button event
16 Oct 2018, 17:32 PM
By AEndumpbub on Reducing Umbraco memory usage by enabling 32-bit applications
13 Oct 2018, 07:04 AM
By nseo-sw.men on MVC forms in Umbraco
12 Oct 2018, 11:30 AM
By JM on Solved: You need to use a Theme.AppCompat theme (or descendant) with this activity
10 Oct 2018, 13:36 PM
By praveen on Xamarin Forms - Working with threads
5 Oct 2018, 12:05 PM
By Jasonced on MVC forms in Umbraco
27 Sep 2018, 05:41 AM
By Евгения on MVC forms in Umbraco
27 Sep 2018, 02:47 AM
By Wokeq62 on Using SQL DataTables to speed up database performance
25 Sep 2018, 17:28 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development