Press Ctrl / CMD + C to copy this to your clipboard.
Copied to clipboard
Flag this post as spam?
This post will be reported to the moderators as potential spam to be looked at
Topic author was deleted
Mar 10, 2020 @ 21:05
Umbraco V8 - Get Current Member in RenderMvcController
Been trying several things but can't seem to:
Check if there is a member logged in
Get the member currently logged in
Anyone get a snippet?
I did manage to find out how to get the backoffice user from a component:
var identity = (UmbracoBackOfficeIdentity)System.Web.HttpContext.Current.User.Identity;
var currentUSer = Current.Services.UserService.GetByProviderKey(identity.Id);
This would help you to fix the issue:
Umbraco V8:
In RenderMvcController, You can directly access MemberHelper. Using this Helper you can get current username.
if (Members.IsLoggedIn())
{
var user = Members.CurrentUserName;
var services = Current.Services.MemberService;
var servicesResult = services.GetByUsername(user);
}
In Umbraco v8 you can also inject services in the controller:
public class MyController : RenderMvcController
{
private readonly IMemberService _memberService;
public MyController(IMemberService memberService)
{
_memberService = memberService;
}
public ActionResult Index()
{
var member = _memberService.GetById(1234);
}
}
or if you don't need to do CRUD operations, just inject MembershipHelper.
public class MyController : RenderMvcController
{
private readonly MembershipHelper _memberHelper;
public MyController(MembershipHelper memberHelper)
{
_memberHelper = memberHelper;
}
public ActionResult Index()
{
var currentMember = _memberHelper.GetCurrentMember();
}
}
Does anyone know if there is an Interface for MembershipHelper? Can't seem to see that anywhere. It would be better if we could inject the abstraction, but it doesn't seem to exist.
Currently there isn't an interface for MembershipHelper and UmbracoHelper, so you would have to inject MembershipHelper or UmbracoHelper or you can access UmbracoHelper via Members / Umbraco properties in controllers inheriting from Umbraco specific controllers.
Topic author was deleted
Umbraco V8 - Get Current Member in RenderMvcController
Been trying several things but can't seem to:
Check if there is a member logged in Get the member currently logged in
Anyone get a snippet?
I did manage to find out how to get the backoffice user from a component:
Hello Tim,
This would help you to fix the issue: Umbraco V8: In RenderMvcController, You can directly access MemberHelper. Using this Helper you can get current username.
Comment author was deleted
Super, thanks for the response #hero
Thank you, Tim. Let me know if any kind of help. I would love to help in Umbraco.
Hi Tim
In Umbraco v8 you can also inject services in the controller:
or if you don't need to do CRUD operations, just inject
MembershipHelper
.I haven't tested it, but I think it should work.
/Bjarne
Comment author was deleted
Thanks! Yeah comes in handy to inject the MembershipHelper :)
Does anyone know if there is an Interface for MembershipHelper? Can't seem to see that anywhere. It would be better if we could inject the abstraction, but it doesn't seem to exist.
Hi Jamie
Currently there isn't an interface for
MembershipHelper
andUmbracoHelper
, so you would have to injectMembershipHelper
orUmbracoHelper
or you can accessUmbracoHelper
viaMembers
/Umbraco
properties in controllers inheriting from Umbraco specific controllers.It is also possible to access
MembershipHelper
viaUmbracoHelper
, but this is now obsolete https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Web/UmbracoHelper.cs#L94-L98 so it is recommended to either injectMembershipHelper
or access it viaMembers
property.In Umbraco vNext I think this will change, but not sure if it going to change in Umbraco v8.
/Bjarne
Great - thanks for your help Bjarne!
MembershipHelper
it is.Cheers,
Jamie
is working on a reply...