| Added: April/22/2006 at 2:47pm | IP Logged
|
|
|
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
|