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;
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?
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 :-)
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...?
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.
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.
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 ;)
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
Hi Dan,
The umbraco.library class is a static one in the umbraco namespace so a single:
should do the trick. You could then just use:
HTH,
Benjamin
EDIT: Saw the bit about cookies and GUIDs, updated answer :-)
Perfect, thank you sir!
In fact, from your edit, what's "UmbracoContext"?
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...?
Thanks Benjamin, but when I use UmbracoContext I get errors saying "The name UmbracoContent does not exist in the current context". Any ideas?
My bad - add this as well:
HTH,
Benjamin
Just a quick note, you can also use the normal asp.net funtion like this:
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
Thanks Chaps,
Benjamin, when I use
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?
is working on a reply...