Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mark Smit 4 posts 85 karma points
    Nov 09, 2015 @ 21:21
    Mark Smit
    1

    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?

    Kind regards, Mark Smit

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Nov 10, 2015 @ 11:07
    Dennis Aaen
    3

    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.

    @if(Umbraco.MemberHasAccess(CurrentPage.Id, CurrentPage.Path)){
        <h1>Welcome!</h1>
    }   
    

    .MemberIsLoggedOn() Returns a Boolean on whether there is currently a member profile

    @if(Umbraco.MemberIsLoggedOn()){
        <h1>Welcome!</h1>
    }
    

    .IsProtected(int pageId, string path) Returns a Boolean on whether a page with a given pageId and path has public access restrictions set.

    @foreach (var child in CurrentPage.Children) { 
        <h2>@child.Name</h2>
            @if(Umbraco.IsProtected(child.id, child.Path)){
                <blink>Members only</blink>
            }
    }
    

    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

  • Mark Smit 4 posts 85 karma points
    Nov 13, 2015 @ 09:06
    Mark Smit
    0

    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

  • Per Olsson 47 posts 307 karma points
    Nov 13, 2015 @ 09:19
    Per Olsson
    0
    @if(umbraco.BusinessLogic.User.GetCurrent() != null) { // do something }
    

    Should do the trick.

  • ZNS 60 posts 198 karma points
    Nov 13, 2015 @ 09:25
    ZNS
    104

    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.

  • Chris Wilson 100 posts 377 karma points
    Aug 10, 2016 @ 10:59
    Chris Wilson
    0

    This works for me, but I second the idea of a simple method returning a Boolean.

  • Per Olsson 47 posts 307 karma points
    Nov 13, 2015 @ 10:00
    Per Olsson
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft