Active InfoActive Info  Display List of Info MembersMemberlist  Search The InfoSearch  HelpHelp
  RegisterRegister  LoginLogin
ASPProtect.NET Authentication Add-On Documentation (Info Locked Info Locked)
 CJWSoft Support Info : ASPProtect.NET Authentication Add-On Documentation
Subject Info: Accessing User Info After Login A d d  -  P o s tAdd P o s t
Author
Message << Prev Info | Next Info >>
cwilliams
Admin Group
Admin Group
Avatar
CJWSoft Web Software Developer

Joined: April/06/2004
Online Status: Offline
Info: 1769
Added: April/22/2006 at 2:47pm | IP Logged Quote cwilliams

Accessing User Info After Login

After a user logs in there are some session variables that you can always access.They can be used to check various things or to display information
dynamically based on who has logged into the system.

In the ASP.NET Authenitcation DLL we are only setting 5 key session variables to conserve server resources. You can grab this info at any time after authentication like so.

HttpContext.Current.Session("User_ID")
HttpContext.Current.Session("Username")
HttpContext.Current.Session("Admin")
HttpContext.Current.Session("Access_Level")
HttpContext.Current.Session("Groups")

If you need to access other field information you can do a database query like so. This code checks to see if we have done this yet by checking a TestSessionUpdate Session Variable used to keep track of that. Then if the session variable for Username has a value we continue. If someone is logged in it will. Then we query the database, and set whatever fields in the database as Session Variables we want to. In the example I am just setting the Last_Name & First_Name fields. (see the code in red) Then we TestSessionUpdate to "true" so the next time this code runs we know the addtional session variables have already been set. No sense doing this over and over and wasting database resources.


This code below is for ASP.NET 2.0..  ASP.NET 1.1 grabs the values from the web.config slightly different. You'll have to modify the code if using ASP.NET 1.1 by simple renaming any ConfigurationManager.AppSettings to ConfigurationSettings.AppSettings


Dim TestSessionUpdate As String
TestSessionUpdate = HttpContext.Current.Session("TestSessionUpdate"
)

If TestSessionUpdate <> "true" Then

  Dim TestSessionUsername As String
  TestSessionUsername = HttpContext.Current.Session("Username"
)

    If TestSessionUsername <> "" Then

      Dim ASPPConnStr As String = ConfigurationManager.AppSettings("ASPPConnStr")
      Dim ASPP_tbl_label_users As String = ConfigurationManager.AppSettings("ASPP_tbl_label_users")

      Dim ASPPdbconn, ASPPsql, ASPPdbcomm, ASPPdbread
      ASPPdbconn = New
OleDbConnection(ASPPConnStr)
      ASPPdbconn.Open()

      ASPPsql = "SELECT " & ASPP_tbl_label_users & ".* FROM " & ASPP_tbl_label_users & " WHERE (Username = '" & TestSessionUsername & "')"

      ASPPdbcomm = New OleDbCommand(ASPPsql, ASPPdbconn)
      ASPPdbread = ASPPdbcomm.ExecuteReader()

      While
ASPPdbread.Read()

         ' Add whatever fields from the database you like here
        HttpContext.Current.Session("First_Name") = ASPPdbread("First_Name")
        HttpContext.Current.Session("Last_Name") = ASPPdbread("Last_Name")

      End While

      ' here we set a session variable so we know things have been
      ' set so this code only runs when it needs to 
      HttpContext.Current.Session("TestSessionUpdate") = "true"

      'Clean up data connections

      ASPPdbread.Close()
      ASPPdbconn.Close()
      ASPPdbconn.Dispose()

    End If

End If



Any page you do this on will need to import "System.Data.OleDb" of course.

Like this for inline code.

<%@ Import Namespace="System.Data.OleDb" %>

Like this if using code.behind.

Imports System.Data.OleDb



__________________

Best Regards, Christopher Williams www.CJWSoft.com
Back to Top View cwilliams's Profile Search for other info by cwilliams Visit cwilliams's Homepage
 
cwilliams
Admin Group
Admin Group
Avatar
CJWSoft Web Software Developer

Joined: April/06/2004
Online Status: Offline
Info: 1769
Added: November/19/2006 at 3:17pm | IP Logged Quote cwilliams

UPDATE:

As of Version 1.2 of the licensing dll you can now optionally have session variables created/saved for more of felds in the database. This should make things a little easier for you if you need them and are having trouble setting them on your own. Please realize if you have a lot of users logged in at once this will use more server memory.

 

This is the new value for the web.config to set the addtional session variables during login.

<!-- Setting below specifies the creation of more session variables during login.
-->

<add key="ASPPUse_Extended_Session_Info" value="true" />

Which will give you access to all of the following as well as the ones that are always set.

HttpContext.Current.Session("First_Name")
HttpContext.Current.Session("Last_Name")
HttpContext.Current.Session("Company_Name")
HttpContext.Current.Session("Email")
HttpContext.Current.Session("Address")
HttpContext.Current.Session("City")
HttpContext.Current.Session("State_Province")
HttpContext.Current.Session("Zipcode_Postal_Code")
HttpContext.Current.Session("Phone")
HttpContext.Current.Session("Expiration_Date")
HttpContext.Current.Session("Custom1")
HttpContext.Current.Session("Custom2")
HttpContext.Current.Session("Custom3")
HttpContext.Current.Session("Custom4")
HttpContext.Current.Session("Custom5")
HttpContext.Current.Session("Custom6")



__________________

Best Regards, Christopher Williams www.CJWSoft.com
Back to Top View cwilliams's Profile Search for other info by cwilliams Visit cwilliams's Homepage
 

Sorry, you can NOT post info.
This info has been locked by a info administrator.

  A d d  -  P o s tAdd P o s t
Printable version Printable version

Info Jump
You cannot add new info in this area
You cannot add to info in this area
You cannot delete your info in this area
You cannot edit your info in this area
You cannot create polls in this area
You cannot vote in polls in this area


Active Server Pages ASP a directory of ASP tutorials applications scripts components and articles for the novice to professional developer. CJWSoft ASPProtect ASPBanner ASPClassifieds www.aspprotect.com, www.powerasp.com,www.aspclassifieds.com,www.aspphotogallery.com,www.codewanker.com