loading roles from DB to auth cookie

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 10
Reputation: jhoop2002 is an unknown quantity at this point 
Solved Threads: 0
jhoop2002 jhoop2002 is offline Offline
Newbie Poster

loading roles from DB to auth cookie

 
0
  #1
Jul 19th, 2005
So ive got my login page almost completed, now for whatever reason i am stuck trying to get the roles for each user from my database into the authorization cookie. In regular old asp this wouldn't be a problem for me, but don't see what im doing wrong in .net

so you can see where i create my ticket, and i hard coded Admin in there, and tested that to make sure everything works, and since it does i need it to be dynamic so when a regular user logs in they will only have user rights.

Here is the code to my login page

  1. <%@ Page Language="VB" %>
  2. <%@ Import Namespace="System.Data" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4. <%@ Import Namespace="System.Threading" %>
  5.  
  6. <script runat="server">
  7. Dim conMyData As SqlConnection
  8. Dim conUserData As SqlConnection
  9. Dim cmdSelect As SqlCommand
  10. Dim cmdSelectRoles As SqlCommand
  11. Dim parmReturnValue As SqlParameter
  12. Dim intResult As Integer
  13. Dim strLinkPath As String
  14. Dim objTicket As FormsAuthenticationTicket
  15. Dim objCookie As HttpCookie
  16. Dim strReturnURL As String
  17.  
  18. Sub Button_Click(ByVal a As Object, ByVal e As EventArgs)
  19. If IsValid Then
  20. 'load stored procedure DBAuthenticate
  21. If DBAuthenticate(txtUsername.Text, txtPassword.Text) > 0 Then
  22. 'create authentication ticket
  23. objTicket = New FormsAuthenticationTicket(2, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(30), False, "Admin")
  24. 'create cookie UserName
  25. Response.Cookies("UserName").Value = txtUsername.Text
  26. objCookie = New HttpCookie(".ASPXAUTH")
  27. objCookie.Value = FormsAuthentication.Encrypt(objTicket)
  28. Response.Cookies.Add(objCookie)
  29. strReturnURL = Request.Params("ReturnURL")
  30. If strReturnURL <> Nothing Then
  31. 'returns user to previous page if greater authorization was required
  32. Response.Redirect(strReturnURL)
  33. Else
  34. 'forwards user after logi
  35. Response.Redirect("role_page.aspx")
  36. End If
  37. End If
  38. End If
  39. End Sub
  40.  
  41. 'check failed login attempt count and if greater than 3 pauses for 2 hours
  42. Sub Page_Load()
  43. Dim objCounter As Object = Session("counter")
  44. If Session("counter") > 3 Then
  45. thread.sleep(7200000)
  46. Response.Redirect("deny.aspx")
  47. End If
  48. End Sub
  49.  
  50. 'stored procedure, returns 1 if successful login, -1 it not
  51. Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
  52. conMyData = New SqlConnection("Server=INTRANET;UID=sa;Database=safety_training")
  53. cmdSelect = New SqlCommand("DBAuthenticate", conMyData)
  54. cmdSelect.CommandType = CommandType.StoredProcedure
  55. parmReturnValue = cmdSelect.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
  56. parmReturnValue.Direction = ParameterDirection.ReturnValue
  57. cmdSelect.Parameters.AddWithValue("@Username", strUsername)
  58. cmdSelect.Parameters.AddWithValue("@Password", strPassword)
  59. conMyData.Open()
  60. cmdSelect.ExecuteNonQuery()
  61. intResult = cmdSelect.Parameters("RETURN_VALUE").Value
  62. conMyData.Close()
  63. 'if unsuccessful login display message and increase failed attempt count by 1 then
  64. 'pauses for 10, then 20, then 30 seconds if user keeps failign
  65. If intResult = -1 Then
  66. lblMessage.Text = "Your Username or Password is incorrect. Please try again."
  67. Dim objCounter As Object = Session("counter")
  68. If objCounter Is Nothing Then objCounter = 0
  69. Session("counter") = CInt(objCounter) + 1
  70. Thread.Sleep(10000 * (CInt(objCounter)))
  71. End If
  72. Return intResult
  73. End Function
  74.  
  75. </script>
  76.  
  77. <html>
  78. <head><title>Login.aspx</title></head>
  79. <body style="text-align: center">
  80. <form id="Form1" runat="server">
  81. &nbsp;<table style="width: 264px; height: 155px;">
  82. <tr>
  83. <td colspan="2" style="height: 43px; text-align: center">
  84. <strong><span style="font-size: 16pt">
  85. Please Login:</span></strong></td>
  86. </tr>
  87. <tr>
  88. <td colspan="2" style="text-align: center">
  89.  
  90. <asp:Label
  91. ID="lblMessage"
  92. ForeColor="Red"
  93. Font-Bold="true"
  94. Runat="server" /></td>
  95. </tr>
  96. <tr>
  97. <td style="width: 60px">
  98. <strong>Username:</strong></td>
  99. <td style="width: 11px">
  100. <asp:TextBox
  101. ID="txtUsername"
  102. Runat="server" Width="160px" /></td>
  103. </tr>
  104. <tr>
  105. <td colspan="2" style="height: 1px">
  106. </td>
  107. </tr>
  108. <tr>
  109. <td style="width: 60px">
  110. <strong>Password:</strong></td>
  111. <td style="width: 11px">
  112. <asp:TextBox
  113. ID="txtPassword"
  114. Runat="server" TextMode="Password" Width="160px" /></td>
  115. </tr>
  116. <tr>
  117. <td colspan="2" style="text-align: right">
  118. <asp:Button
  119. Text="Login"
  120. OnClick="Button_Click"
  121. Runat="server" ID="Button1" /></td>
  122. </tr>
  123. </table>
  124. <hr>
  125. </form>
  126. </body>
  127. </html>
  128.  
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: loading roles from DB to auth cookie

 
0
  #2
Jul 20th, 2005
Have you tried creating a function that makes a call to the DB 'where UserName = ' & txtUserName.Text and return that value to a string variable you pass into the authentication.ticket method?
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: jhoop2002 is an unknown quantity at this point 
Solved Threads: 0
jhoop2002 jhoop2002 is offline Offline
Newbie Poster

Re: loading roles from DB to auth cookie

 
0
  #3
Jul 21st, 2005
yes, it works with "Admin" hard coded in there, but i am new to .net and still learning everything. Looking at that code, I don't know how to get my stored proceedure to return the users role. I tried writing another one, but it didn't work out to well. I guess this just comes down to writing a query and converting the results to a string and then inserting them into that ticket.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: jhoop2002 is an unknown quantity at this point 
Solved Threads: 0
jhoop2002 jhoop2002 is offline Offline
Newbie Poster

Re: loading roles from DB to auth cookie

 
0
  #4
Jul 21st, 2005
Thanks for at least trying to help, but some of the people at aspmessageboard.com helped me out after i took your advice.

Here is the code that made it all work for me. This goes in the login page;

    Sub Button_Click(ByVal a As Object, ByVal e As EventArgs)
        If IsValid Then
            'load stored procedure DBAuthenticate
            If DBAuthenticate(txtUsername.Text, txtPassword.Text) > 0 Then
                Dim conRoles As SqlConnection
                Dim cmdSelectRoles As SqlCommand
                Dim dtrRoles As String

                conRoles = New SqlConnection("Server=INTRANET;uid=sa;database=safety_training")
                conRoles.Open()
                cmdSelectRoles = New SqlCommand("SELECT g.name FROM dbo.Groups g WHERE g.group_id IN (SELECT r.group_id FROM dbo.Roles r WHERE r.user_id IN (SELECT ui.user_id FROM dbo.User_Info ui WHERE ui.user_name=@username AND ui.password=@password))", conRoles)
                cmdSelectRoles.Parameters.AddWithValue("@username", txtUsername.Text)
                cmdSelectRoles.Parameters.AddWithValue("@password", txtPassword.Text)
                dtrRoles = cmdSelectRoles.ExecuteScalar

                'create authentication ticket
                objTicket = New FormsAuthenticationTicket(2, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(30), False, dtrRoles)
                conRoles.Close()

Now since I don't know how to get this page to redirect depending on the role of the user, I redirected to another page and had that page check the role of the user and then they are redirected to the page i want them to start out at.
Reply With Quote Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: loading roles from DB to auth cookie

 
0
  #5
Jul 22nd, 2005
Good work.

Just an FYI (understanding .NET, doesn't excuse from needing an understanding of SQL)

pseudo-code

CREATE PROCEDURE sp_GetRole /* How it would appear in QUERY ANALYZER */
	(
		@UserName VARCHAR(50) = NULL,
		@Password VARCHAR(50) = NULL,
		@Role VARCHAR(20) = NULL 
	)
AS
	SET @Role = (SELECT Role
	FROM UserTable /* or what you called you table with this data */
	WHERE UserName = @UserName AND Password = @Password)
RETURN @Role

Hope this helps!

Glad to see you found another option.
Assistant Manager, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC