Copied to clipboard

Flag this post as spam?

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


  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2010 @ 16:41
    Dan
    0

    Determine if member is logged in with ASP.NET

    Hi,

    An easy one for the .NET-heads no doubt, but I can only find XSLT documentation about this and am looking to do it in a .NET usercontrol instead.

    Basically I just want to find out if a user is logged in, and if not, set a cookie with a GUID value if one hasn't already been set.

    The code I have so far is as follows, but I have no idea how to check whether the member is logged in or not, and if not, how to generate the GUID value for the cookie.  I also have no idea if I'm going about this the right way ;)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace MyProject
    {
    public partial class SetCookie : System.Web.UI.UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (umbraco.library.IsLoggedOn())
    {
    Label1.Text = "Logged in";
    }
    else
    {
    Label1.Text = "Not logged in.";

    if (Request.Cookies["visitorGUID"] == null)
    {
    Label1.Text += "No cookie, so creating one";
    Response.Cookies["visitorGUID"].Value = "GUID-STRING-1234567890";
    Response.Cookies["visitorGUID"].Expires = DateTime.Now.AddDays(1);
    }
    else
    {
    Label1.Text += "Cookie already set";
    }
    }
    }
    }
    }

    Presumably if "umbraco.library.IsLoggedOn" is the right thing to use here, I'll need to include a reference to an umbraco namespace at the top of the file, in which case, which one?

    Any pointers for a .NET dummy much appreciated.

    Thanks

  • Jamie Howarth 306 posts 773 karma points c-trib
    Aug 30, 2010 @ 17:42
    Jamie Howarth
    0

    Hi Dan,

    The umbraco.library class is a static one in the umbraco namespace so a single:

    using umbraco;

    should do the trick. You could then just use:

    if (library.IsLoggedOn()) {
    // Do whatever with your logged-in user here
    } else {
    // Note use of Request object to retrieve existing value, if present...
    if (string.IsNullOrEmpty(UmbracoContext.Current.Request.Cookies["mycookie"].Value) {
    Guid myguid = Guid.NewGuid();
    // ...and Response object to set a cookie's value.
    UmbracoContext.Current.Response.Cookies["mycookie"].Value = myguid.ToString();
    }
    }

    HTH,

    Benjamin

    EDIT: Saw the bit about cookies and GUIDs, updated answer :-)

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2010 @ 17:51
    Dan
    0

    Perfect, thank you sir!

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2010 @ 17:56
    Dan
    0

    In fact, from your edit, what's "UmbracoContext"?

  • Jamie Howarth 306 posts 773 karma points c-trib
    Aug 30, 2010 @ 18:05
    Jamie Howarth
    0

    UmbracoContext is a class that represent the HttpContext for Umbraco. I can't remember the specifics as to why it was put into Umbraco 4.5 but I do remember it was to do with flaws in using HttpContext.Current from the System.Web namespace - I think it was to do with out-of-context API calls...?

  • Dan 1288 posts 3921 karma points c-trib
    Aug 30, 2010 @ 19:08
    Dan
    0

    Thanks Benjamin, but when I use UmbracoContext I get errors saying "The name UmbracoContent does not exist in the current context".  Any ideas?

  • Jamie Howarth 306 posts 773 karma points c-trib
    Aug 30, 2010 @ 19:10
    Jamie Howarth
    0

    My bad - add this as well:

    using umbraco.presentation;

    HTH,

    Benjamin

  • Hendrik Jan 71 posts 137 karma points
    Aug 30, 2010 @ 20:56
    Hendrik Jan
    0

    Just a quick note, you can also use the normal asp.net funtion like this:

    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated){ ... }

     

  • Jamie Howarth 306 posts 773 karma points c-trib
    Aug 31, 2010 @ 00:22
    Jamie Howarth
    1

    Niels,

    It's recommended to use the new UmbracoContext class in place of HttpContext due to the reasons I mentioned above. It inherits from HttpContext so has all the same stuff in there.

    Best,

    Benjamin

  • Dan 1288 posts 3921 karma points c-trib
    Aug 31, 2010 @ 10:52
    Dan
    0

    Thanks Chaps,

    Benjamin, when I use

    UmbracoContext.Current.Request.Cookies["mycookie"].Value

    For example, I get more errors in the build:

    Error    2    The type 'System.Web.HttpRequestWrapper' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

    Error    3    'umbraco.presentation.UmbracoRequest' does not contain a definition for 'Cookies' and no extension method 'Cookies' accepting a first argument of type 'umbraco.presentation.UmbracoRequest' could be found (are you missing a using directive or an assembly reference?)

     

    I tried to add 'using System.Web.Abstractions' but the last part (Abstractions) doesn't appear in the intellisense selector, so was a bit wary of doing that.  If I add it, the above two errors go away, but I get a new error:

    Error    2    The type or namespace name 'Abstractions' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

    I think I'm just missing some namespace references in this whole thing, but have no idea how to find out which ones.

    Any ideas?

     

Please Sign in or register to post replies

Write your reply to:

Draft