Ok, is there no way to do this from the config file in case i need to change it in future? (I suppose i could add a variable to the config file to read from) but just in case there is this option?
No idea if this is the right way of doing it, but here's something else you can do to access additional member properties:
@inject IMemberManager MemberManager
@inject IMemberService MemberService
@{
var currUser = MemberManager.GetCurrentMemberAsync().GetAwaiter().GetResult();
if (currUser != null)
{
var usersname = currUser.Name;
var member = MemberService.GetById(int.Parse(currUser.Id));
var someValue = member.GetValue<string>("someProperty");
}
}
It seems silly to me that there would need to be two services involved in accessing the details of the currently logged in member, so somebody more informed can feel free to correct this.
Interesting, that does seem to work. Seems a bit like the difference between a published content and a non-published content entity.
That is, MemberManager.AsPublishedMember returns a "published" member that you can access data with (including in a typed way if you cast the IPublishedContent to a models-builder generated class for that member type, such as Member).
And MemberService.GetById returns a non-published member that you can set data on (e.g., memberInstance.SetValue("firstName", "SomeFirstName")).
It will use calls straight to the DB and is very slow compared to IMemberManager.
Its recommanded to never use IContentService or IMemberService in views or anywhere the client can call it.
There are multiple ways of doing this but here is one where we use IMemberManager to check if there is a member loggedin and then we fetch the member. Then we can also use IMemberManager to fetch the IPublishedContent version of the member to access the umbraco content.
if (_memberManager.IsLoggedIn())
{
var currentMember = await _memberManager.GetCurrentMemberAsync();
if (currentMember != null)
{
var publishedMember = _memberManager.AsPublishedMember(currentMember);
var accessUmbracoProperty = publishedMember.Value<string>("someAlias");
}
}
How to get logged in member details?
I have created a member using the admin office and see it listed under members.
How could i do the following (in Razor)
Thanks
Where do you want to do this? In a view or in a controller?
Use the IMemberService to get the Member you should then be able to get the Name property
In a View/Razor page.
Could i do this in the View as well?
Ok, is there no way to do this from the config file in case i need to change it in future? (I suppose i could add a variable to the config file to read from) but just in case there is this option?
You should be able to do the following to get the currently logged in users name in a view
Not that I am aware of
No idea if this is the right way of doing it, but here's something else you can do to access additional member properties:
It seems silly to me that there would need to be two services involved in accessing the details of the currently logged in member, so somebody more informed can feel free to correct this.
You can just use the membermanager
member = MemberManager.AsPublishedMember(currUser)
Interesting, that does seem to work. Seems a bit like the difference between a published content and a non-published content entity.
That is,
MemberManager.AsPublishedMember
returns a "published" member that you can access data with (including in a typed way if you cast theIPublishedContent
to a models-builder generated class for that member type, such asMember
).And
MemberService.GetById
returns a non-published member that you can set data on (e.g.,memberInstance.SetValue("firstName", "SomeFirstName")
).Very aware of how long it's been since last reply but found this Implementation to be quite clean:
Hi Luke,
Be very aware on how you use the MemberService.
It will use calls straight to the DB and is very slow compared to IMemberManager.
Its recommanded to never use IContentService or IMemberService in views or anywhere the client can call it.
There are multiple ways of doing this but here is one where we use IMemberManager to check if there is a member loggedin and then we fetch the member. Then we can also use IMemberManager to fetch the IPublishedContent version of the member to access the umbraco content.
is working on a reply...