Storing the UserID instead of the UserName in the User.Identity.Name Object when using the Login Control and ASP.net Membership API

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.

kick it on DotNetKicks.com

Tags:

ASP.net

Comments

4/22/2009 12:36:55 AM #

trackback

Storing the UserID instead of the UserName in the User.Identity.Name O

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

4/22/2009 12:44:13 AM #

Lee Dumond

Good Lord man, why would you go to all this trouble, when the ID is plainly accessible from the ProviderUserKey property?

msdn.microsoft.com/.../...ser.provideruserkey.aspx

P.S. Welcome to the Wrox family by the way. ;)

Lee Dumond United States

4/26/2009 7:28:34 PM #

elbandit

Hi Lee,

The reason why I went to all of this trouble was because I wanted the id of the user and not user name without having to pull back the entire user from the database which would happen when using ProviderUserKey property. Do you see where I am coming from?
Scott

P.S.
Thanks, I haven't had a chance to download your ASP.net Exceptions Blox yet but thought your Linqify Beer House one was top banana. I am working on a new book with another author at the moment as well:

Professional Enterprise .NET
www.amazon.com/.../0470447613

elbandit United Kingdom

5/15/2009 10:43:50 PM #

North Wales dentists

I believe it corresponds to a part of session variable in a way, also useful to populate the LOGON_USER variable when you use any authentication mode.

North Wales dentists United Kingdom

5/16/2009 5:00:23 AM #

pilkington glass uk

It is indeed a great resource to obtain information on this subject. Keep posting. Thanks

pilkington glass uk United Kingdom

5/17/2009 9:23:38 AM #

Brighouse Dentist

Very good solutions and ideas for better results.

Brighouse Dentist United Kingdom

5/18/2009 2:29:21 AM #

confessionblues

hi nice to meet you. thank's for info share

regard

confessionblues Indonesia

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen