ASP.NET - How can I cache user details for the duration of their visit?

Posted by rockinthesixstring on Stack Overflow See other posts from Stack Overflow or by rockinthesixstring
Published on 2010-06-15T05:34:30Z Indexed on 2010/06/15 5:52 UTC
Read the original article Hit count: 166

Filed under:
|

I've built a Repository that gets user details

Public Function GetUserByOpenID(ByVal openid As String) As User Implements IUserRepository.GetUserByOpenID
    Dim user = (From u In dc.Users
               Where u.OpenID = openid
               Select u).FirstOrDefault
    Return user
End Function

And I'd like to be able to pull those details down IF the user is logged in AND IF the cached data is null.

What is the best way to create a User object that contains all of the users details, and persist it across the entire site for the duration of their visit?

I Was trying this in my Global.asax, but I'm not really happy using Session variables. I'd rather have a single object with all the details inside.

Private Sub BaseGlobal_AcquireRequestState(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.AcquireRequestState
    If Session("UserName") Is Nothing AndAlso User.Identity.IsAuthenticated Then
        Dim repo As UrbanNow.Core.IUserRepository = New UrbanNow.Core.UserRepository
        Dim _user As New UrbanNow.Core.User
        _user = repo.GetUserByOpenID(User.Identity.Name)

        Session("UserName") = _user.UserName()
        Session("UserID") = _user.ID
    End If
End Sub

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about caching