I just upgraded a new project from Umb 7.0.3 to 7.1.0 and now Visual Studio is telling me that the way I get current user is deprecated. Specifically, this:
umbraco.BusinessLogic.User.GetCurrent()
now is tagged with:
Warning:
'umbraco.BusinessLogic.User' is obsolete: "Use the UserService instead"
Which I'm attempted to do however I can find no way to get the current user via the UserService. I see methods to get users by their id, their username, their email address, etc. Which are all great but don't help me as I don't have any of that information. I just want to get the currently logged in user for error logging purposes. What am I overlooking?
var userService = new UserService(new RepositoryFactory());
This will give access to the user service - I use IOC to pass in a userService but you can work that part out yourself.
Once you have this userService look at the methods you can call - I don't see on for the current logged in member.
Personal I grab the user by email - DL the source code form git and have a look through the services and see if another class has a method that matches your needs.
@Charles Afford ... I'm asking about the current User, not the current Member hence my using Umbraco's new UserService. But you're correct, I need to get the current User from the .Net membership provider and then use the UserService to get the ID. Here's what I ended up doing (wrapped in a helper method):
private int GetCurrentUserId()
{
var userService = ApplicationContext.Current.Services.UserService;
return userService.GetByUsername(HttpContext.Current.User.Identity.Name).Id;
}
@Dan Mothersole ... I know how to get the UserService instance and I have already looked through the available methods, hence my question as to how to do what I'm wanting to do. Also, I have downloaded the source and picked through it as well. Some interesting things in there, but didn't find the answer to what I was looking for.
I used Umbraco.Web.UmbracoContext.Current.Security.CurrentUser to obtain current backoffice user. It was works in older Umbraco versions.
But it does not works in "Umbraco 7.2.4".
how to get the current user with UserService
I just upgraded a new project from Umb 7.0.3 to 7.1.0 and now Visual Studio is telling me that the way I get current user is deprecated. Specifically, this:
now is tagged with:
Which I'm attempted to do however I can find no way to get the current user via the UserService. I see methods to get users by their id, their username, their email address, etc. Which are all great but don't help me as I don't have any of that information. I just want to get the currently logged in user for error logging purposes. What am I overlooking?
Hi Chester,
Sorry no one replied. Hope this does not put you of the forum :)
You can take a look at: http://www.charlesafford.com/umbraco-membership-provider.aspx
That should give you the basic idea. Basically you want to use the .net membership API.
Are you trying to get the member (logged in to the umbraco backend) or the member (user logged into the website)?
If your answer you should use the MemberService()
MemberService memberservice = new MemberService()
Charlie :)
using Umbraco.Core.Services;
using Umbraco.Core.Persistence;
var userService = new UserService(new RepositoryFactory());
This will give access to the user service - I use IOC to pass in a userService but you can work that part out yourself.
Once you have this userService look at the methods you can call - I don't see on for the current logged in member.
Personal I grab the user by email - DL the source code form git and have a look through the services and see if another class has a method that matches your needs.
@Charles Afford ... I'm asking about the current User, not the current Member hence my using Umbraco's new UserService. But you're correct, I need to get the current User from the .Net membership provider and then use the UserService to get the ID. Here's what I ended up doing (wrapped in a helper method):
@Dan Mothersole ... I know how to get the UserService instance and I have already looked through the available methods, hence my question as to how to do what I'm wanting to do. Also, I have downloaded the source and picked through it as well. Some interesting things in there, but didn't find the answer to what I was looking for.
How about the following within the Umbraco.Web namespace?
Sorry Chester never got a notifcation you posted
You can do this..
var current = UmbracoContext.Current;
var user = current.UmbracoUser;
I thnk that in Umbraco 7.2.4 there is a problem.
I used
Umbraco.Web.UmbracoContext.Current.Security.CurrentUser
to obtain current backoffice user. It was works in older Umbraco versions.But it does not works in "Umbraco 7.2.4".
How can I solve it?
Thanks
I want to know if the back office user is logged in on a front end template, I'm using umbraco 7.2.8
None of the above work for me, so I've reverted to:
See why here: http://issues.umbraco.org/issue/U4-6496
I'm using this code:
I am running 7.4.3 and I can use to get the current user in a class inheriting from UmbracoAuthorizedJsonController.
I'm using Umraco 7.4.3 as well and discovered that the current user is null right after login in backoffice.
However, the user is null only when using UmbracoContext.Current. When using umbraco.BusinessLogic then the user is not null.
After a while (30 seconds or 1 minute maybe), I try to read user1 and user2 again and they are not null anymore!
Is there a caching issue regarding the authenticated user?
Umbraco.UmbracoContext.Security.CurrentUser;
this works for me.
is working on a reply...