Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
I am trying to get the current logged on member in an api controller, what code should I use? tried searching but not getting anywhere.
Hi Huw
I'm thinking that there is access to the MembershipHelper from UmbracoApiControllerBase that UmbracoApiController inherits from:
https://github.com/umbraco/Umbraco-CMS/blob/2bfef741914297c802bc6c77cc48780d7534f920/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs#L135
The property is called 'Members', and the MembershipHelper has a GetCurrentMember() method:
https://github.com/umbraco/Umbraco-CMS/blob/b8540ca128bf1622ff26eb21d331577776d5a725/src/Umbraco.Web/Security/MembershipHelper.cs#L401
So I reckon you should be able to write something like:
var currentMember = Members.GetCurrentMember();
think it'll be null though if nobody is logged in for the context of the request...
regards
Marc
It returns null even when logged in, also tried Members.GetCurrentMemberId and that always returns -1
And what is the v9 GetCurrentMember?
GetCurrentMember
:-(
What happens if you add the members authorize atribute to the controller?
https://our.umbraco.com/documentation/reference/routing/webapi/authorization#using-memberauthorizeattribute
?
I'll give that a try.
didn't help, but have got around it by passing the username to my api method to achieve what I required
In v10+ , you can use:
_userService.GetByEmail(HttpContext.User.Identity.Name);
Where _userService is of type IUserService (using Umbraco.Cms.Core.Services)
_userService
IUserService
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
How do I get the current member in UmbracoApiController
I am trying to get the current logged on member in an api controller, what code should I use? tried searching but not getting anywhere.
Hi Huw
I'm thinking that there is access to the MembershipHelper from UmbracoApiControllerBase that UmbracoApiController inherits from:
https://github.com/umbraco/Umbraco-CMS/blob/2bfef741914297c802bc6c77cc48780d7534f920/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs#L135
The property is called 'Members', and the MembershipHelper has a GetCurrentMember() method:
https://github.com/umbraco/Umbraco-CMS/blob/b8540ca128bf1622ff26eb21d331577776d5a725/src/Umbraco.Web/Security/MembershipHelper.cs#L401
So I reckon you should be able to write something like:
think it'll be null though if nobody is logged in for the context of the request...
regards
Marc
It returns null even when logged in, also tried Members.GetCurrentMemberId and that always returns -1
And what is the v9
GetCurrentMember
?:-(
What happens if you add the members authorize atribute to the controller?
https://our.umbraco.com/documentation/reference/routing/webapi/authorization#using-memberauthorizeattribute
?
I'll give that a try.
didn't help, but have got around it by passing the username to my api method to achieve what I required
In v10+ , you can use:
Where
_userService
is of typeIUserService
(using Umbraco.Cms.Core.Services)is working on a reply...