Best way to get user id using EF and storing for the application life span RRS feed

  • Question

  • User36582938 posted

    I am tyring to over come a issue with using session by having a property to store the providerkey i seen examples of this but I am using form authentication so i dont no if i can use that

     Guid _UserID;
    public Guid UserID
    		MembershipUser user = Membership.GetUser(HttpContext.Current.User.Identity.Name);
    		Guid guid;
    		Debug.Assert(user.ProviderUserKey != null, "user.ProviderUserKey != null");
    		guid = (Guid)user.ProviderUserKey;
    		return guid;
    		this._UserID = value;

    The erorr I am getting is the following

    An exception of type 'System.NullReferenceException' occurred in SkyLarkArenaCms.dll but was not handled in user code
    Additional information: Object reference not set to an instance of an object.

    I would reference the property via the master page so that all the child pages can use it so then in my child page i just do this for example to get userId

    lblCount.Text = _dal.GetUserMessageCount(UserID).ToString();
    lblminiCount.Text = lblCount.Text;

    Now I have checked the user does have a userID GUID and its not null or blank.

    <authentication mode="Forms">
      <forms loginUrl="~/BackDoorLogin.aspx"></forms>

    What is the best method to handle the userId I have seen the issues with the session and now want to solve it with a 2015 solution allot of the old ways our outdated I am using asp.net webforms and version .net 4.5.2

    I have reigsterd my sql tables with aspnetreg_iis so i have my aspnetuser table etc in sql server 2008 r2 thanks for in advance for your support and help

    Wednesday, December 16, 2015 6:00 PM

All replies

  • User753101303 posted


    On which line does it happen? You have to check all objects that are used on the line it happens. You are 100% sure user or even lblCount is not null etc...?

    Wednesday, December 16, 2015 6:55 PM
  • User36582938 posted

    its on this line does this work when you have forms authtentication ?

    MembershipUser user = Membership.GetUser(HttpContext.Current.User.Identity.Name);
    Wednesday, December 16, 2015 7:27 PM
  • User20640938 posted

    Wow, that's amazing thank you sir.


    Monday, December 28, 2015 9:11 AM