Copied to clipboard

Flag this post as spam?

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


  • jonok 297 posts 658 karma points
    Aug 17, 2009 @ 22:57
    jonok
    0

    C# code to check if the user is logged on

    I'm implementing the CK Finder file manager with Umbraco, and I need to check if the user is authenticated before I give them access. I've found loads of examples of how to check for authentication in xslt, but none in a C# user control. I'm guessing its fairly simple - this is as close as I got: umbraco.library.isloggedOn()

    Anybody know how? Thanks.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Aug 18, 2009 @ 00:29
    Aaron Powell
    3

    You can do this via the ASP.NET Membership:

    bool isLoggedOn = HttpContext.Current.User.Identity.IsAuthenticated;

    http://msdn.microsoft.com/en-us/library/system.security.principal.iidentity.isauthenticated.aspx

  • Paul Blair 466 posts 731 karma points
    Aug 18, 2009 @ 01:14
    Paul Blair
    1

    I have been using this:

    if (Membership.GetUser() != null)
    {
    ...
    }

  • jonok 297 posts 658 karma points
    Aug 18, 2009 @ 12:51
    jonok
    0

    I've tried both of those and they are still returning false. I'm testing it within the 'umbraco/plugins/tinymce3/insertLink.aspx' popup that is accessed via the tinyMCE interface. For testing purposes I'm just trying to write the value into the HTML using this code:

    <%="test=" + (Membership.GetUser() != null)%>

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Aug 18, 2009 @ 13:23
    Dirk De Grave
    2

    hm, i'm not sure if those methods are supported when working in the backend. also think both aaron and paul were thinking you're integrating that file manager in the front end of the site (which is where the membership stuff becomes more relevant)

    users and members are two completely different concepts in umbraco. users are used to access the admin backend whilst members are people that have registered with your site and get privileges based on their roles.

    so, i'm thinking you'll need to have a closer look at users, and what api is available in that area...

     

    cheers,

    /Dirk

  • Richard Soeteman 4052 posts 12925 karma points MVP 2x
    Aug 18, 2009 @ 13:55
    Richard Soeteman
    1

    If you want to retrieve the current User in the backend you can use the static method

    User.GetCurrent()

    I think this is supported from 4.0.2.1. Not sure. But in the Backend you should always be logged in isn't it? I've used this method to check if a user has access to a certain application.

    Cheers,

    Richard

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Aug 18, 2009 @ 14:04
    Aaron Powell
    1

    Yeah I was assuming that you were looking at front-end not back-end, but it *should* work, I'll check tomorrow as back-end users are also ASP.NET Membership provider based so it should have worked... should have :P

  • jonok 297 posts 658 karma points
    Aug 18, 2009 @ 15:16
    jonok
    0

    Richard - I tried your piece of code, it returned the following error:

    CS1061: 'System.Security.Principal.IPrincipal' does not contain a definition for 'GetCurrent' and no extension method 'GetCurrent' accepting a first argument of type 'System.Security.Principal.IPrincipal' could be found (are you missing a using directive or an assembly reference?)

    Do I need to cast the User as a type that is used within Umbraco?

  • jonok 297 posts 658 karma points
    Aug 20, 2009 @ 10:36
    jonok
    1

    I found the solution after digging around google a bit more, it checks if the back-end umbraco user is authenticated:

    if (umbraco.BasePages.UmbracoEnsuredPage.CurrentUser != null)

     

     

     

  • Richard Soeteman 4052 posts 12925 karma points MVP 2x
    Aug 21, 2009 @ 09:06
    Richard Soeteman
    0

    Great,

    Still strange that the User.GetCurrent doesn't work. why is it made static then? Thanks for your feedback

    Cheers,

    Richard

  • Arjan den Boer 76 posts 31 karma points
    Nov 17, 2009 @ 12:20
    Arjan den Boer
    0

    User.GetCurrent works fine in a User Control in v 4.0.2.1

  • Richard Soeteman 4052 posts 12925 karma points MVP 2x
    Nov 17, 2009 @ 13:39
    Richard Soeteman
    0

    Yes, it's implemented in 4.0.2.1 so that should work ;-)

Please Sign in or register to post replies

Write your reply to:

Draft