I currently have this code in my view for testing purposes, and it works:
@if (Umbraco.MemberIsLoggedOn())
{
var user = System.Web.Security.Membership.GetUser();
if (user != null)
{
var m = ApplicationContext.Services.MemberService.GetByUsername(user.UserName);
var testProperty = "";
if (m.HasProperty("testProperty") && m.GetValue("testProperty") != null)
{
testProperty = m.GetValue("testProperty").ToString();
}
<p>User: @user.UserName is logged in</p>
<p>Test property: @testProperty</p>
}
}
My question is: Is this the simplest way to do it? It seems kind of unnecessary to use the MemberService to get another type of user (IMember instead of MembershipUser) just to access custom properties. But I can't see any methods on the MembershipUser to access custom properties. Am I just missing something? Or is this how you're meant to do it?
Edit: As per @Tim's answer below, the following code is much better:
@if (Umbraco.MemberIsLoggedOn())
{
var currentUser = Members.GetCurrentMember();
if (currentUser != null)
{
var testProperty = currentUser.GetPropertyValue<string>("testProperty");
<p>User: @currentUser.Name is logged in</p>
<p>Test property: @testProperty</p>
}
}
You should in theory be able to just do:
@Members.GetCurrentMember()
Which should return the current member as IPublishedContent, and then you can access the member properties in the same way as with published content, e.g. GetPropertyValue("propertyAlias")
.