473,405 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Form Authentication Ticket to Store additional User Data

50
I am trying to implement login page using Form Authentication in ASP.net using vb code.I follow the steps in How To Implement Forms-Based Authentication in Your ASP.NET Application by Using Visual Basic .NET. I created a function in login page :


Expand|Select|Wrap|Line Numbers
  1. Private Function ValidateUser(ByVal strUsername As String, ByVal strPassword As String) As Boolean
  2.  
and call it in btnLogIn_Click

Expand|Select|Wrap|Line Numbers
  1. Protected Sub btnLogIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogIn.Click
  2.         If ValidateUser(txtUserName.Text, txtPassword.Text) Then
  3.             Dim tkt As FormsAuthenticationTicket
  4.             Dim strCookie As String
  5.             Dim ck As HttpCookie
  6.  
  7.             tkt = New FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now(), _
  8.                   DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "my user data")
  9.             strCookie = FormsAuthentication.Encrypt(tkt)
  10.             ck = New HttpCookie(FormsAuthentication.FormsCookieName(), strCookie)
  11.             If (chkPersistCookie.Checked) Then ck.Expires = tkt.Expiration
  12.             ck.Path = FormsAuthentication.FormsCookiePath()
  13.             Response.Cookies.Add(ck)
  14.  
  15.             Dim strRedirect As String
  16.             strRedirect = "MaintainUsers.aspx"
  17.             Response.Redirect(strRedirect, True)
  18.  
  19.         Else
  20.             Response.Redirect("Login.aspx", True)
  21.         End If
  22.     End Sub
  23.  
  24.  

and in masterpage page_load , the usename is displayed in the welcome message


Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.      If HttpContext.Current.User.Identity.IsAuthenticated Then
  3.           Me.lblWelcomeMessage.Text = "Welcome," + " " + HttpContext.Current.User.Identity.Name
  4.      End If
  5. End Sub
  6.  
Things works fine and i can get the username display in label.

However,I need to get more user data like UserName, Fullname and RoleCode. Also, i would to display the user's fullname to instead of usename in welcome message. I was told this can be done using FormsAuthenticationTicket method to store addictional user data in the "my user data" section. Do i need to create a user data class to store the user data and then use it in the FormAuthenticationTicket? If yes, how should i do it? I have been scratching my head several days in googling to get a proper guide to do this,but i still cannot find out the solution.

Please can anyone help me? I am quite lost now , your help is much appreciated. Thank you.
Aug 16 '11 #1
1 3780
beemomo
50
Dear all,

I manage to store the additional use data (username, fullname, rolecode) in FormAuthenticationTicket. However ,
the user has more than one role , he can be admin, poweruser , executive ,etc... can anyone please tell me how can i concatenate the rodecode return by datareader if it returns more than 1 value? so that i can put it as a string in userdatastring of the authentication ticket?


Expand|Select|Wrap|Line Numbers
  1. drDataReader = cmd.ExecuteReader()
  2. While drDataReader.Read()
  3.  
  4. strFullName = drDataReader("FullName").ToString
  5. strUserName = drDataReader("UserName").ToString
  6. strRoleCode = drDataReader("RoleCode").ToString
  7.  
  8. userDataString = String.Concat(strFullName, "|", strUserName, "|", strRoleCode)
  9.  
  10. Dim authCookie As HttpCookie = FormsAuthentication.GetAuthCookie(txtUserName.Text, chkPersistCookie.Checked)
  11.  
  12. Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt(authCookie.Value)
  13.  
  14. Dim newTicket As FormsAuthenticationTicket = New FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userDataString)
  15.  
  16. ' Update the authCookie's Value to use the encrypted version of newTicket
  17.  
  18. authCookie.Value = FormsAuthentication.Encrypt(newTicket)
  19.  
  20. ' Manually add the authCookie to the Cookies collection
  21. Response.Cookies.Add(authCookie)
  22.  
  23. ' Determine redirect URL and send user there
  24. Dim strRedirect As String
  25. strRedirect = "MainTainUsers.aspx"
  26. Response.Redirect(strRedirect, True)
  27. End While
  28.  
Aug 16 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ben S | last post by:
framework 1.1 in our webapp, we are using forms authentication. ================= Auth Section from web.config ================= <authentication mode="Forms"> <forms name="loginauth"...
1
by: e | last post by:
I'm using forms authentication on a site. When the user logs in via the login page, the entered creds are checked against AD, and if valid, an encrypted forms authentication ticket is produced and...
0
by: francois | last post by:
hello, I am using forms authentication and I would like that my authentication cookie expires after let say 1 minutes (just for the exemple). When I log in in my longon page, the user has to...
3
by: Martin | last post by:
Dear fellow ASP.NET programmer, I stared using forms authentication and temporarily used a <credentials> tag in web.config. After I got it working I realized this wasn't really practical. I...
2
by: Beginner | last post by:
I know this is an old question, but searching all over the internet plus several MS security conferences, still haven't got a straight anwser. Basically, the login.aspx is on one dedicated server...
0
by: Sean Patterson | last post by:
Hey all, I've followed the examples online on how to use Forms Authentication to create a ticket, assign it a role, and then intercept it in the Global.asax file to make sure it gets sucked in...
3
by: chuck rudolph | last post by:
Folks, Can anyone confirm that my understading is correct and maybe shed some light on why it's as it is. (I'm guessing security, but that seems weak to me.) The asp.net web application is using...
3
by: JIM.H. | last post by:
Hello, I used this site to do form authentication http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secmod/html/secmod17.asp My code successfully comes to the point: if...
5
by: Rob Kay | last post by:
Hello. I would like to know what is the easiest and safest way to extend the default MemberShip Provider for SQL Server 2005 to include additional user data (eg HomePhone, City, State etc). ...
10
by: anjummir | last post by:
Hello, I am trying to develop custom form based authentication with active directory with asp.net 2.0 platform. I am having difficulty trying to implement security model. Here is what my senerio...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.