Login used to work

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

Login used to work

 
0
  #1
Dec 21st, 2005
I worked on this site over the summer at an office, I'm now back on christmas break, and will work again this summer. With the help of many people online I got the following code to work. But now, it doesn't work. As far as I know, the code SHOULD still be the same. I am using VS2005 instead of VWD beta, and the 2.0 framework instead of the beta version.

Outline of what the below code should do:
Take user login information, pass it to storedprocedure DBAuthenticate, if valid user, then execute another query to retrieve role info for forms authentication and store it in the authentication ticket. As well as pause for every missed attempt.

What happens:
The login page will load correctly, but when you hit the submit button i get the following error.

"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.


Exception Details: System.ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."



MASTERPAGE(Anonymous.master):
  1. <%@ Master Language="VB" CodeFile="Anonymous.master.vb" Inherits="Anonymous" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" >
  6. <head runat="server">
  7. <title>Untitled Page</title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. <table style="width: 740px; height: 720;" border="1">
  13. <tr>
  14. <td colspan="3" style="height: 87px">
  15. <asp:Image ID="Image1" runat="server" ImageUrl="~/images/top_data/background.jpg" /></td>
  16. </tr>
  17. <tr>
  18. <td colspan="3" style="height: 236px">
  19. <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  20. </asp:contentplaceholder>
  21. &nbsp;
  22. </td>
  23. </tr>
  24. </table>
  25. </div>
  26. </form>
  27. </body>
  28. </html>

Login.aspx:
  1. <%@ Page Language="VB" MasterPageFile="~/Anonymous.master"
  2. AutoEventWireup="false"
  3. CodeFile="Login.aspx.vb"
  4. Inherits="_Default"
  5. EnableEventValidation="true"
  6. title="Untitled Page" %>
  7.  
  8. <%@ Import Namespace="System.Data" %>
  9. <%@ Import Namespace="System.Data.SqlClient" %>
  10. <%@ Import Namespace="System.Threading" %>
  11.  
  12. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  13.  
  14. <script runat="server">
  15. Dim conMyData As SqlConnection
  16. Dim conUserData As SqlConnection
  17. Dim cmdSelect As SqlCommand
  18. Dim cmdSelectRoles As SqlCommand
  19. Dim parmReturnValue As SqlParameter
  20. Dim intResult As Integer
  21. Dim strLinkPath As String
  22. Dim objTicket As FormsAuthenticationTicket
  23. Dim objCookie As HttpCookie
  24. Dim strReturnURL As String
  25.  
  26. Sub Button_Click(ByVal a As Object, ByVal e As EventArgs)
  27. If IsValid Then
  28. 'load stored procedure DBAuthenticate
  29. If DBAuthenticate(txtUsername.Text, txtPassword.Text) > 0 Then
  30. 'run query to get user role for forms based authentication
  31. Dim conRoles As SqlConnection
  32. Dim cmdSelectRoles As SqlCommand
  33. Dim dtrRoles As String
  34.  
  35. conRoles = New SqlConnection("Server=INTRANET;uid=sa;database=safety_training")
  36. conRoles.Open()
  37. cmdSelectRoles = New SqlCommand("SELECT r.role_name FROM dbo.Roles r WHERE r.role_id IN (SELECT ui.role_id FROM dbo.User_Info ui WHERE ui.user_name=@username AND ui.password=@password)", conRoles)
  38. cmdSelectRoles.Parameters.AddWithValue("@username", txtUsername.Text)
  39. cmdSelectRoles.Parameters.AddWithValue("@password", txtPassword.Text)
  40. dtrRoles = cmdSelectRoles.ExecuteScalar
  41.  
  42. 'run query to get users company
  43. Dim conCompany As SqlConnection
  44. Dim cmdSelectCompany As SqlCommand
  45. Dim dtrCompany As String
  46.  
  47. conCompany = New SqlConnection("Server=INTRANET;uid=sa;database=safety_training")
  48. conCompany.Open()
  49. cmdSelectCompany = New SqlCommand("SELECT ui.company_id FROM dbo.User_Info ui WHERE ui.user_name=@username", conCompany)
  50. cmdSelectCompany.Parameters.AddWithValue("@username", txtUsername.Text)
  51. dtrCompany = cmdSelectCompany.ExecuteScalar
  52.  
  53. 'create authentication ticket
  54. objTicket = New FormsAuthenticationTicket(2, txtUsername.Text, DateTime.Now, DateTime.Now.AddMinutes(30), False, dtrRoles)
  55. conRoles.Close()
  56. conCompany.Close()
  57.  
  58. 'create cookie Company
  59. Response.Cookies("Company").Value = dtrCompany
  60. 'create cookie UserName
  61. Response.Cookies("UserName").Value = txtUsername.Text
  62.  
  63. objCookie = New HttpCookie(".ASPXAUTH")
  64. objCookie.Value = FormsAuthentication.Encrypt(objTicket)
  65. Response.Cookies.Add(objCookie)
  66. strReturnURL = Request.Params("ReturnURL")
  67.  
  68. If strReturnURL <> Nothing Then
  69. 'returns user to previous page if greater authorization was required
  70. Response.Redirect(strReturnURL)
  71. Else
  72. 'forwards user after login
  73. Response.Redirect("Default.aspx")
  74. End If
  75. End If
  76. End If
  77. End Sub
  78.  
  79. 'check failed login attempt count and if greater than 3 pauses for 2 hours
  80. Sub Page_Load()
  81. Dim objCounter As Object = Session("counter")
  82. If Session("counter") > 3 Then
  83. Thread.Sleep(7200000)
  84. Response.Redirect("anonymous/deny.aspx")
  85. End If
  86. End Sub
  87.  
  88. 'stored procedure, returns 1 if successful login, -1 it not
  89. Function DBAuthenticate(ByVal strUsername As String, ByVal strPassword As String) As Integer
  90. conMyData = New SqlConnection("Server=INTRANET;UID=sa;Database=safety_training")
  91. cmdSelect = New SqlCommand("DBAuthenticate", conMyData)
  92. cmdSelect.CommandType = CommandType.StoredProcedure
  93. parmReturnValue = cmdSelect.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
  94. parmReturnValue.Direction = ParameterDirection.ReturnValue
  95. cmdSelect.Parameters.AddWithValue("@Username", strUsername)
  96. cmdSelect.Parameters.AddWithValue("@Password", strPassword)
  97. conMyData.Open()
  98. cmdSelect.ExecuteNonQuery()
  99. intResult = cmdSelect.Parameters("RETURN_VALUE").Value
  100. conMyData.Close()
  101. 'if unsuccessful login display message and increase failed attempt count by 1 then
  102. 'pauses for 10, then 20, then 30 seconds if user keeps failign
  103. If intResult = -1 Then
  104. lblMessage.Text = "Your Username or Password is incorrect. Please try again."
  105. Dim objCounter As Object = Session("counter")
  106. If objCounter Is Nothing Then objCounter = 0
  107. Session("counter") = CInt(objCounter) + 1
  108. Thread.Sleep(10000 * (CInt(objCounter)))
  109. End If
  110. Return intResult
  111. End Function
  112.  
  113. </script>
  114. <form id="Form1" action="Login.aspx" >
  115. <table style="width: 41%; border-right: #b5c7de 1px solid; border-top: #b5c7de 1px solid; border-left: #b5c7de 1px solid; border-bottom: #b5c7de 1px solid; height: 1px;">
  116. <tr align="center" valign="top">
  117. <td bgcolor="#eff3fb" style="text-align: center; width: 843px; height: 175px;">
  118. <table>
  119. <tr>
  120. <td colspan="2"
  121. style="text-align: center">
  122. <strong><span style="font-weight: bold;
  123. color: black;
  124. background-color: transparent; font-family: Verdana;">Log In</span></strong></td>
  125. </tr>
  126. <tr align="center">
  127. <td colspan="2">
  128. <asp:Label
  129. ID="lblMessage"
  130. ForeColor="Red"
  131. Runat="server" /></td>
  132. </tr>
  133. <tr valign="top">
  134. <td style="width: 85px">
  135. <asp:Label ID="UserNameLabel"
  136. runat="server" Font-Names="Verdana" Font-Size="0.8em">User Name:</asp:Label>
  137. </td>
  138. <td style="width: 173px">
  139. <asp:TextBox ID="txtUsername"
  140. runat="server"
  141. Width="160px" TabIndex="1" /><br />
  142.  
  143. <asp:RequiredFieldValidator ID="RequiredFieldValidator1"
  144. runat="server"
  145. ControlToValidate="txtUsername"
  146. Text="You must enter a User Name." Width="186px" EnableViewState="False" Font-Names="Verdana" Font-Size="0.7em" /></td>
  147. </tr>
  148. <tr valign="top">
  149. <td style="width: 85px">
  150. <asp:Label ID="PasswordLabel"
  151. runat="server" Font-Names="Verdana" Font-Size="0.8em">Password:</asp:Label>
  152. </td>
  153. <td style="width: 173px">
  154. <asp:TextBox ID="txtPassword"
  155. runat="server"
  156. TextMode="Password"
  157. Width="160px" TabIndex="2" /><br />
  158.  
  159. <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
  160. runat="server"
  161. ControlToValidate="txtPassword"
  162. Text="You must enter a Password." Width="235px" EnableViewState="False" Font-Names="Verdana" Font-Size="0.7em" /></td>
  163. </tr>
  164. <tr align="center">
  165. <td colspan="2" style="height: 32px; text-align: right">
  166. <asp:Button ID="Button1"
  167. runat="server"
  168. BackColor="White"
  169. BorderColor="#507CD1"
  170. BorderStyle="Solid"
  171. BorderWidth="1px"
  172. OnClick="Button_Click"
  173. Font-Names="Verdana"
  174. Font-Size="0.8em"
  175. ForeColor="#284E98"
  176. Text="Log In" TabIndex="3" /></td>
  177. </tr>
  178. </table>
  179. <span style="font-size: 0.8em; color: red"></span></td>
  180. </tr>
  181. </table>
  182. </form>
  183. </asp:Content>

Global.asax:
  1. <%@ Application Language="VB" %>
  2.  
  3. <script runat="server">
  4.  
  5. Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
  6. If Not (HttpContext.Current.User Is Nothing) Then
  7. If HttpContext.Current.User.Identity.IsAuthenticated Then
  8. If TypeOf HttpContext.Current.User.Identity Is FormsIdentity Then
  9. Dim id As FormsIdentity = CType(HttpContext.Current.User.Identity, FormsIdentity)
  10. Dim ticket As FormsAuthenticationTicket = id.Ticket
  11. Dim userData As String = ticket.UserData
  12. Dim roles As String() = userData.Split(",")
  13. HttpContext.Current.User = New System.Security.Principal.GenericPrincipal(id, roles)
  14. End If
  15. End If
  16. End If
  17. End Sub
  18.  
  19. Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
  20. ' Code that runs on application startup
  21. End Sub
  22.  
  23. Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
  24. ' Code that runs on application shutdown
  25. End Sub
  26.  
  27. Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
  28. ' Code that runs when an unhandled error occurs
  29. End Sub
  30.  
  31. Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
  32. ' Code that runs when a new session is started
  33. End Sub
  34.  
  35. Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
  36. ' Code that runs when a session ends.
  37. ' Note: The Session_End event is raised only when the sessionstate mode
  38. ' is set to InProc in the Web.config file. If session mode is set to StateServer
  39. ' or SQLServer, the event is not raised.
  40. End Sub
  41.  
  42. </script>

Web.Config:
  1. <?xml version="1.0"?>
  2. <!-- Note: As an alternative to hand editing this file you can use the
  3. web admin tool to configure settings for your application. Use
  4. the Website->Asp.Net Configuration option in Visual Studio.
  5. A full list of settings and comments can be found in
  6. machine.config.comments usually located in
  7. \Windows\Microsoft.Net\Framework\v2.x\Config
  8. -->
  9. <configuration>
  10. <!-- ASP.NET connection string to connect to
  11. Microsoft SQL Server INTRANET, database Safety_Training -->
  12. <connectionStrings>
  13. <add name="Safety_TrainingConnectionString"
  14. connectionString="Data Source=INTRANET; Initial Catalog=Safety_Training; User ID=sa"
  15. providerName="System.Data.SqlClient"/>
  16. </connectionStrings>
  17.  
  18. <system.web>
  19. <!-- Authorization section sets access for anonymous users throughout site -->
  20. <authorization>
  21. <deny users="?"/>
  22. </authorization>
  23.  
  24. <!-- Sets Authentication Method for site, Forms based and denies access
  25. to folder (root) for non authenticated users
  26.  
  27. The <authentication> section enables configuration
  28. of the security authentication mode used by
  29. ASP.NET to identify an incoming user -->
  30.  
  31. <authentication mode="Forms">
  32. <forms name=".ASPXAUTH"
  33. loginUrl="login.aspx"
  34. protection="All"
  35. path="/"
  36. timeout="5"/>
  37. </authentication>
  38.  
  39. <!-- The <customErrors> section enables configuration
  40. of what to do if/when an unhandled error occurs
  41. during the execution of a request. Specifically,
  42. it enables developers to configure html error pages
  43. to be displayed in place of a error stack trace.
  44.  
  45. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
  46. <error statusCode="403" redirect="NoAccess.htm" />
  47. <error statusCode="404" redirect="FileNotFound.htm" />
  48. </customErrors> -->
  49. <customErrors mode="Off"/>
  50.  
  51. <!-- Set compilation debug="true" to insert debugging
  52. symbols into the compiled page. Because this
  53. affects performance, set this value to true only
  54. during development -->
  55. <compilation debug="true"/>
  56.  
  57. <!-- Defines the sitemap provider for the navigation controls -->
  58.  
  59. </system.web>
  60. <appSettings/>
  61. </configuration>
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Login used to work

 
0
  #2
Jan 12th, 2006
This is a new security feature for .net2. <%@ Page EnableEventValidation="true" %> is new security feature to stop tampering with your controls. in your code you changed some controls on the server side and then tried to load the page and it recognised they were changed in your event. At the end of your event call Response.redirect("login.aspx") and it will solve the problem for you.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC