How to check if an umbraco user is logged in on a frontend request
To restrict access to a page I'd like to check if an umbraco user is logged on. In my RenderMvcController I tried the following:
var loggedOn = umbraco.library.IsLoggedOn();
var getCurrentUser = umbraco.BusinessLogic.User.GetCurrent();
var getCurrentUmbracoUser = umbraco.helper.GetCurrentUmbracoUser();
var securityCurrentUser = UmbracoContext.Security.CurrentUser;
var umbracoEnsuredPageCurrentUser = umbraco.BasePages.UmbracoEnsuredPage.CurrentUser;
However, all of the above return null or false regardless of a user being logged into umbraco or not. What is the correct API call to check this?
Thank you for your reply. However I'm not interested in Members, I want to know if an umbraco (backend!) User is logged in.
As an addition: I also tried annotating mycontroller action with [Umbraco.Web.WebApi.UmbracoAuthorize] but no success (always no access, regardless of a User being logged into umbraco or not).
for 7.2.8+ the only thing I've got to work is this:
using Umbraco.Core.Security;
var ticket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
if (ticket != null) {
var user = Umbraco.Core.ApplicationContext.Current.Services.UserService.GetByUsername(ticket.Name);
}
I'd prefer to use some user api, but those I've tried have all returned null.
How to check if an umbraco user is logged in on a frontend request
To restrict access to a page I'd like to check if an umbraco user is logged on. In my RenderMvcController I tried the following:
However, all of the above return null or false regardless of a user being logged into umbraco or not. What is the correct API call to check this?
Kind regards, Mark Smit
Hi Mark,
Have you seen the documentation for the UmbracoHelpers, there you will find some when you are working with members. https://our.umbraco.org/documentation/Reference/Querying/UmbracoHelper/
.MemberHasAccess(int nodeId, string path); Returns a Boolean on whether the currently logged in member has access to the page given its ID and path.
.MemberIsLoggedOn() Returns a Boolean on whether there is currently a member profile
.IsProtected(int pageId, string path) Returns a Boolean on whether a page with a given pageId and path has public access restrictions set.
And then we also have the member service that you can find the documentation here https://our.umbraco.org/documentation/reference/management/services/memberservice
Hope this helps,
/Dennis
Dear Dennis,
Thank you for your reply. However I'm not interested in Members, I want to know if an umbraco (backend!) User is logged in.
As an addition: I also tried annotating mycontroller action with [Umbraco.Web.WebApi.UmbracoAuthorize] but no success (always no access, regardless of a User being logged into umbraco or not).
I hope somebody can help me.
Kind regards, Mark Smit
Should do the trick.
for 7.2.8+ the only thing I've got to work is this:
using Umbraco.Core.Security; var ticket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket(); if (ticket != null) { var user = Umbraco.Core.ApplicationContext.Current.Services.UserService.GetByUsername(ticket.Name); }
I'd prefer to use some user api, but those I've tried have all returned null.This works for me, but I second the idea of a simple method returning a Boolean.
ZNS is correct, as of 7.2.4+ you can no longer use Umbraco's "CurrentUser" method from the front-end (it's however used by the backoffice).
http://issues.umbraco.org/issue/U4-6496
http://issues.umbraco.org/issue/U4-6342
is working on a reply...