CodeVerge.Net Beta


   Explore    Item Entry    Members      Register  Login  
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML

Free Download:




Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.security Tags:
Item Type: NewsGroup Date Entered: 8/1/2007 3:14:38 AM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 12 Views: 34 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
13 Items, 1 Pages 1 |< << Go >> >|
MrCoastline
Asp.Net User
Store User Info into Session8/1/2007 3:14:38 AM

0/0

I can't figure out why I am getting this dreaded error when trying to store the currented loggedin user into a session?  Or please post the correct code for accomplishing this task.  Thx.

ERROR: Object reference not set to an instance of an object. (occurs on the first line assigning a value to session)

Imports
System.Web.Security.MembershipUser
Imports System.Web.Profile
Imports System.web.SessionState

Partial Class Login
Inherits System.Web.UI.Page
Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
Dim User As MembershipUser
User = Membership.GetUser()
HttpContext.Current.Session(
"UserGUID") = User.ProviderUserKey.ToString
HttpContext.Current.Session(
"UserName") = User.UserName.ToString
End Sub

 


-- "Mark As Answer" If my reply helped you --
gprina
Asp.Net User
Re: Store User Info into Session8/1/2007 4:03:54 AM

0/0

"User" is probably equal to "Nothing"


Guillermo Prina

Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
MrCoastline
Asp.Net User
Re: Store User Info into Session8/1/2007 6:53:59 AM

0/0

If the user is not available at this point, then what is the correct code to capture the login information (at login) and store it into session?


-- "Mark As Answer" If my reply helped you --
gprina
Asp.Net User
Re: Store User Info into Session8/1/2007 4:22:45 PM

0/0

GetUser() only works for authenticated users, check with User.Identity.IsAuthenticated if the user was really authenticated.

If the user was authenticated and you still obtaining Nothing with GetUser() that means that the "username" of the authenticated user was not found on the database (or the datasource you are using).


Guillermo Prina

Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
MrCoastline
Asp.Net User
Re: Store Logged In User into Session8/2/2007 2:42:52 AM

0/0

When I check IsAuthenticated it is false.   Okay, so I don't understand, this is the Login control and the LoggedIn event, which one would think means the user is now logged in and properly authenticated.

Anyway my end game is to just obtain the correct code that will correctly grab the logged in user so I can put them into session variables.  If I can't do it here then when and where can I do it during the login process? This really sounds like something that many people would want to do so there must be an easy solution out there.


-- "Mark As Answer" If my reply helped you --
Torqie
Asp.Net User
Re: Store User Info into Session8/2/2007 3:31:40 AM

0/0

Try this code:

 Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
Dim User As MembershipUser
If Page.User.Identity.IsAuthenticated = True Then
User = Membership.GetUser()
HttpContext.Current.Session(
"UserGUID") = User.ProviderUserKey.ToString
HttpContext.Current.Session(
"UserName") = User.UserName.ToString
end If
End Sub

 

MrCoastline
Asp.Net User
Re: Store User Info into Session8/2/2007 3:49:01 AM

0/0

Thanks. That is the exact code I have now, so I am preplexed as to why in this event the IsAuthenticated cond reports false - yet the page logs me in just fine.  I login just fine but can't grab the logged in user information to store it into Session.  So this leads me to suspect that for some reason the LoggedIn event is not appropriate for this action?   I would love to understand why and and also how to achieve my goal.


-- "Mark As Answer" If my reply helped you --
Torqie
Asp.Net User
Re: Store User Info into Session8/2/2007 11:11:18 PM

0/0

MrCoastline:

Thanks. That is the exact code I have now, so I am preplexed as to why in this event the IsAuthenticated cond reports false - yet the page logs me in just fine.  I login just fine but can't grab the logged in user information to store it into Session.  So this leads me to suspect that for some reason the LoggedIn event is not appropriate for this action?   I would love to understand why and and also how to achieve my goal.

 

 Try this, instead of using the built in login control try using your own text boxes and button have the textbox ID's be txt_username, and txt_password then run this code under the button click event of the button that you create with what ever ID you want to give it

 ' This code will auth a person wether they use their email address they signed up 
        'or with or with the username those chose.

        'First Dim something to put the login info in
        Dim LoginID As String
        'Now check wether they entered in an email address or username
        'and if entered in email address it will populate LoginID with the username
        'that goes with that email address
        If Membership.Provider.GetUserNameByEmail(txt_username.Text) Is Nothing Then
            LoginID = txt_username.Text
        Else
            LoginID = Membership.Provider.GetUserNameByEmail(txt_username.Text)
        End If
        'Next check the username and password and if correct it will login if not lbl will be shown and told
        'that they do not have the correct login.
        If (Membership.ValidateUser(LoginID, txt_password.Text)) Then
            FormsAuthentication.RedirectFromLoginPage(LoginID, True)
        Else
            lbl_result.Text = "Incorrect Login"
        End If

 

And then put this code under the Page_load event

 'Also User might be a already used by asp.net so you may want to change it 
        'to something like AuthedUser

        Dim AuthedUser As MembershipUser
        If Page.User.Identity.IsAuthenticated = True Then
            AuthedUser = Membership.GetUser()
            HttpContext.Current.Session("UserGUID") = AuthedUser.ProviderUserKey.ToString
            HttpContext.Current.Session("UserName") = AuthedUser.UserName.ToString
        End If
 
Torqie
Asp.Net User
Re: Store User Info into Session8/3/2007 5:07:43 AM

0/0

Actually.. I think I have it figured out.. when you use the getuser method you need to specify how to get the user You are not supplying it with anything so try this line of code in there

 

AuthedUser = Membership.GetUser(page.user.identity.name)
          

gprina
Asp.Net User
Re: Store Logged In User into Session8/3/2007 7:25:42 PM

0/0

MrCoastline:

When I check IsAuthenticated it is false.   Okay, so I don't understand, this is the Login control and the LoggedIn event, which one would think means the user is now logged in and properly authenticated.

Probably that part of your code is correct, you should check elsewhere. Verify the method you are using for authentication (in web.config), etc.

Check this article, it could help you:

http://www.codeproject.com/useritems/logincontrol.asp


Guillermo Prina

Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
MrCoastline
Asp.Net User
Re: Store User Info into Session8/4/2007 5:56:20 AM

0/0

Torqie, your comments sparked something that I read somewhere, but can't seem to locate again now.  You have to specifically call ValidateUser with username/pwd the user entered even though this is the LoggedIn event, which fires after the user has already been validated.  I know, it defies logic, so if some expert can explain this counter intuitive behavior then I will be able to sleep better at night.  In the end I came up with the following code that works and will hopefully prevent others from banging their heads into walls.

This does not require modifying the Login control or intercepting any button events.  Just a few lines of tight code.

Enjoy,
Victor

Partial Class Login
Inherits System.Web.UI.Page

Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
Dim AuthedUser As MembershipUser
Dim strUserName As String = CType(Login1.FindControl("UserName"), TextBox).Text
Dim strPassword As String = CType(Login1.FindControl("Password"), TextBox).Text
If Membership.ValidateUser(strUserName, strPassword) Then
AuthedUser = Membership.GetUser(strUserName)
HttpContext.Current.Session(
"UserGUID") = AuthedUser.ProviderUserKey.ToString
HttpContext.Current.Session(
"UserName") = AuthedUser.UserName.ToString
End If
End Sub

End Class


-- "Mark As Answer" If my reply helped you --
plillevold
Asp.Net User
Re: Store User Info into Session9/14/2007 10:19:13 AM

0/0

Victor,

actually the Membership.ValidateUser is not necessary. This is done by the Login control.

I think the problem must be something else. Because, if you inspect the session that is used to store UserGUID and UserName you will see that on the page loaded after the LoggedIn event is done the session is cleared. Or rather, a new session is started because the session id's of the HttpContext.Current.Session in LoggedIn and on subsequent requests are different.

So my question is, what happens with the session after LoggedIn ?? Using the Session_Start event in Global.asax I can clearly see that a new session is actually started after the LoggedIn event is done and before the next page is processed, thus invalidating any data stored in session during LoggedIn.

 

- Peter
 


- Peter
plillevold
Asp.Net User
Re: Store User Info into Session9/14/2007 10:29:11 AM

0/0

...and for a solution I found that the Session_Start event in Global.asax could serve me well. Heres a sample:

 

    void Session_Start(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser();
        if (user != null)
        {
            HttpContext.Current.Session["UserName"] = user.UserName;
             // ...or store some other user-related data in session here
        }
    }

 

- Peter 


- Peter
13 Items, 1 Pages 1 |< << Go >> >|


Free Download:

Books:
Rails Solutions: Ruby on Rails Made Easy Authors: Justin Williams, Pages: 268, Published: 2007
Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Authors: William Steinmetz, Brian Ward, Pages: 197, Published: 2008
Core Internet Application Development with ASP.NET 2.0 Authors: Randy Connolly, Pages: 1049, Published: 2007
Patterns of Enterprise Application Architecture Authors: Martin Fowler, David Rice, Pages: 560, Published: 2003
HTTP Developer's Handbook Authors: Chris Shiflett, Pages: 282, Published: 2003
Professional Apache Tomcat 5 Authors: Vivek Chopra, Chanoch Wiggers, Inc NetLibrary, Pages: 598, Published: 2004
Performance Analysis for Java Web Sites Authors: Stacy Joines, Ruth Willenborg, Ken Hygh, Pages: 425, Published: 2002
Advanced Rails Authors: Brad Ediger, Pages: 357, Published: 2007
Guide to J2EE: Enterprise Java Authors: John Hunt, Chris Loftus, Pages: 672, Published: 2003
Instant Coldfusion 5 Authors: Jeffry Houser, Pages: 690, Published: 2001

Web:
Cornel Creanga » Blog Archive » Flex, Blaze DS and storing session ... Aug 21, 2008 ... After a successfully login authentication I have to store the user ID into the server session. After that this ID is used to check all the ...
9.10 How to Store Information About the Current User Session If you need to store information related to the current user session in a way ... Example 9-16 Setting Information into the UserData Hashtable for Access By ...
The Tomcat 4 Servlet/JSP Container - Sharing session data between ... Next, I store the shared data (user roles and groups in my case) into an Hashtable ... This certainly prevents you from storing shared session data into a ...
赖洪礼的 blog » Rails 2.0, cookie session store and security CookieStore is implemented by serializing session data into a cookie. .... All I try to store in session ever is User ID and Flash messages. ...
Capturing User Session Information Capturing User Session Information. The data that ODBC logging collects is obviously .... To put the data into the session log in SQL Server, we use ADO. ...
Storing user and database connection information in Session ... I am going to store this user connection info(user id, encrypted password and ... and database server) into an arrayList Session variable. ...
PHP Programming/sessions - Wikibooks, collection of open-content ... Jun 9, 2008 ... If it does exist, the PHP script will attempt to store the file's data into _SESSION variable for further use ...
Why you probably shouldn't use cookies to store session data Aug 12, 2008 ... This introduces more complexity into your application and increases ... Store the user id in a server-side session store such as a database ...
Using XML to Store Session Data -RSS I understand how to put data into and retreive data from an SQL database. It would be nice, however, to see an example of how to "store all session ...
PHP Programming » Blog Archive » Using PHP Session Control To ... May 12, 2008 ... At the simple end, we’d store the user name into their session and assume we know the user is who they logged in as. ...

Videos:
Book Search a great cause & Desktop a computing shift http://www.uberpulse.com/us/2007/08/marissa_mayer_favourite_googles_service_book_search_igoogle.php During her Q&A session at the Search Engine Stra...
Data Access in the ASP.NET 2.0 Framework www.mylivelessons.com Stephen Walther, the world's leading ASP.NET authority introduces his new LiveLesson- DVD-ROM hands-on personal video instruct...
الجهاد PLEASE READ VERY CAREFULLY THESE TERMS AND CONDITIONS AND THE PROGRAM FREQUENTLY ASKED QUESTIONS LOCATED ON THE PROGRAM WEBSITE AT https://upload.vi...
Developing and deploying an application on Google App Engine This video introduces developers to building apps on Google App Engine. For more in-depth information and deep-dive technical sessions, come to Googl...
Fogos 2008 ! PLEASE READ VERY CAREFULLY THESE TERMS AND CONDITIONS AND THE PROGRAM FREQUENTLY ASKED QUESTIONS LOCATED ON THE PROGRAM WEBSITE AT https://upload.v...
الله اكبر PLEASE READ VERY CAREFULLY THESE TERMS AND CONDITIONS AND THE PROGRAM FREQUENTLY ASKED QUESTIONS LOCATED ON THE PROGRAM WEBSITE AT https://upload.vi...
tutty shih tzu PLEASE READ VERY CAREFULLY THESE TERMS AND CONDITIONS AND THE PROGRAM FREQUENTLY ASKED QUESTIONS LOCATED ON THE PROGRAM WEBSITE AT https://upload....
قدرة الله قوية PLEASE READ VERY CAREFULLY THESE TERMS AND CONDITIONS AND THE PROGRAM FREQUENTLY ASKED QUESTIONS LOCATED ON THE PROGRAM WEBSITE AT https://upload.vi...
sunil bharathi mittal chairman involved in illegal activities-CBCID ENQUIRY ordered and requested for CBI enquiry Before the Honorable Thiru. Mukarjee, Director General of Police, Dr. Radhakrishnan Salai, Mylapore, Chennai -V...
The Archimedes Palimpsest Google TechTalks March 7, 2006 Will Noel Roger L. Easton, Jr. Michael B. Toth ABSTRACT The Archimedes Palimpsest is a 10th Century medieval manuscr...




Search This Site:










controll id duplicate problem

why i get an error when displaying a xml file?

autoscalebasesize -> autoscaledimensions

multiple dnn sites on a "sandbox" testing server, different ports not working

3.0.5 issue - upgrade from 2.1.2 requires localhost/dotnetnuke as the only listed portalalias

how can i hide the datagrid?

string character problem when calling update command

response in a new window

document versioning (like sharepoint)

prevent a particular role from using the close verb in a web part

re: jumping into asp.net article by amundsen

visual studio 2005 cannot work with secondary webs on the server 2003

portal skin change to host skin sometimes 3.0.12

need help with .ascx controls layout (please)

dnn 2.0.4 and dotnetwiki

aspx page not refreshing

is it possible to implement the paging concept in a datagrid without going back to the server

dnn4 install??? --- where are instructions

double-precision floating-point numbers

secured application

adding user to role (vb)

question re: walkthrough: data binding web pages with a visual studio data component

something about webconfig ??

re: tutorial for .net platform

stripping out html tags for plain txt email

siteurls.config secret

can't access menus

function file ?

set isauthenticated to true

tabstrip client events?

 
All Times Are GMT