开发者

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

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
        Di开发者_如何学运维m _user As New UrbanNow.Core.User
        _user = repo.GetUserByOpenID(User.Identity.Name)

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


Store the full user object in a session variable instead of just members.

//store
Session("UserInfo") = _user

//read
Dim user = DirectCast(Session("UserInfo"), UrbanNow.Core.User)

(Code is converted to vb.net by online converter, i don't know any vb.net so sorry for any syntax errors)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜