by elbandit
5. June 2009 19:11
The problem I had wiith using the Membership API and the login Control was that by default after logging in the User.Identity.Name is set to the username that users logon with, and this is usually a string value i.e. their email address or username.
I wanted to store the user id in the User.Identity.Name varaible, this is how I achieved it using the Membership Login Control:
Imports System.Web.Security
Imports System.Web.UI.WebControls
Imports MySite.UserService
Partial Class MembersArea_CustomerLogin
Inherits System.Web.UI.Page
Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
Dim user As User
' The UserService is the interface into my business layer
Dim aUserService As New UserService
' This method simply returns the UserID of the user that has just been
' logged in. We get the Username from the login control text box.
user = aUserService.GetUserBy(Login1.UserName)
' Now that we have got the UserID we need to recreate the
' authentication ticket we removed earlier otherwise
' the user will not be authenticated.
Dim authTicket As FormsAuthenticationTicket = _
New FormsAuthenticationTicket(user.Id , False , 30)
Dim encryptedTicket As String = FormsAuthentication.Encrypt(authTicket)
Dim authCookie As HttpCookie = _
New HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
HttpContext.Current.Response.Cookies.Add(authCookie)
End Sub
End Class
Now we can access the UserID where ever we want.
Dim productService As New ProductService
Dim allProjects As List(Of Projects)
allProjects = productService.GetAllProjectsBy(CType(User.Identity.Name, Long))
Marvellous.

cb257e1f-230a-4625-9f2e-d7659e38869a|2|5.0
Tags:
ASP.net