Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1199 posts 2567 karma points
    Dec 13, 2011 @ 00:55
    Amir Khan
    0

    add username to session variable

    Hi,

    What is the best way to add a Member name to a session variable? I'm trying to set it up so I can direct them to specific pages base on name, here's the .NET code i nave so far.

    Thanks!

    Amir

     

    public override bool CheckAuthentication()
    {
    // WARNING : DO NOT simply return "true". By doing so, you are allowing
    // "anyone" to upload and list the files in your server. You must implement
    // some kind of session validation here. Even something very simple as...
    //
    //
    //
    // ... where Session[ "IsAuthorized" ] is set to "true" as soon as the
    // user logs on your system.
    if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)

    {
    return true;
    }

    return false;

    }

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Dec 13, 2011 @ 02:55
    Bo Damgaard Mortensen
    0

    Hi Amir,

    You should be able to set the session upon login. In your Login controls OnLoggingIn (or OnLoggedIn) you could maybe do this:

    Session["UserName"] = HttpContext.Current.User.Identity.Name;

    This stores the username of the logged in member as a session variable.

    - Bo

  • Amir Khan 1199 posts 2567 karma points
    Dec 13, 2011 @ 21:41
    Amir Khan
    0

    Hi Bo, thanks for your help, so something like this?

     

    public void OnLoggingIn()
    {
    Session["UserName"] = HttpContext.Current.User.Identity.Name;
    }

    Then retrieve it like this?

    (string)Session["UserName"];
  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Dec 14, 2011 @ 00:56
    Bo Damgaard Mortensen
    0

    Hi Amir,

    yes, that would be my guess :-) does it work?

    Alternatively you could also just check on either HttpContext.Current.User.Identity.Name; or Member.GetCurrent().LoginName;  before redirecting.

    - Bo

  • Amir Khan 1199 posts 2567 karma points
    Dec 15, 2011 @ 02:54
    Amir Khan
    0

    Sure did! These are the two bits I used, thank you for your help!

    Session["UserName"] = HttpContext.Current.User.Identity.Name; 
    (string)Session["UserName"];

    -Amir

     





  • Amir Khan 1199 posts 2567 karma points
    Dec 15, 2011 @ 05:15
    Amir Khan
    0

    Bo,

    Do you have a good resource for getting other properties like role name? I'm having trouble searching for it, not sure if im just using the wrong terminology.

    Thanks!

    Amir

  • Bo Damgaard Mortensen 712 posts 1189 karma points
    Dec 17, 2011 @ 00:06
    Bo Damgaard Mortensen
    0

    Hi Amir,

    Yes, you should be able to find everything you need when it comes to Roles/UserGroups in the Roles class found in the System.Web.Security assembly. Here's an overview of the class:

    http://msdn.microsoft.com/en-us/library/system.web.security.roles.aspx

    Hope that helps :) If not, let me know!

    - Bo

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies