G
Guest
I have an app that was originally 1.1, now migrated to 2.0 and have run into
some sporadic viewstate errors...usually saying the viewstate is invalid,
eventvalidation failed or mac error.
My web config does specify a machinekey setting:
<machineKey
validationKey="447C05E8B3A71401CC4CAE5513A7F1A3494A3618EE819316AAD1D58433F236A759D66FB4154500E01EB4E1BC1DE42046E2D652D391CB8367A1649438867A02EB"
decryptionKey="CE8D47C43312A144B49DE5E8D3D3CA2CDEA230077AFB86CB"
validation="SHA1"/>
The errors are occuring during some custom code that i wrote that saves form
data when a user's login times out and restores it by reposting it after the
user logs in.
Specifically, in the global.asax file there is a function as below
(truncated to shorten). When the user's session times out from inactivity,
the entire request.form object (all posted data) is saved in the cache with a
key saved as a cookie on the user's machine.
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires upon attempting to authenticate the user
If (Request.IsAuthenticated) Then
........................
ElseIf (Request.Form.Count > 0 AndAlso
(Request.Cookies("widsplusformkey") Is Nothing OrElse
Request.Cookies("widsplusformkey").Value = "") AndAlso Request.Path.ToLower
<> "/widsplus/login.aspx" AndAlso Request.Path.ToLower <> "login.aspx") Then
Dim guidstring As String = Guid.NewGuid.ToString
' Save any posted form data
System.Web.HttpContext.Current.Cache.Add(guidstring,
Request.Form, Nothing, Now.AddMinutes(16), Cache.NoSlidingExpiration,
Caching.CacheItemPriority.Normal, Nothing)
' save cookie with guid string
Response.Cookies("widsplusformkey").Value = guidstring
Response.Cookies("widsplusformkey").Path = "/widsplus"
Response.Cookies("widsplusformkey").Expires =
Now().AddMinutes(15)
End If
End Sub
Next, on the logon page, once the user is authenticated, it redirects
manually to another page called "formredirect.aspx". The code on that page
(below) custom generates a page reconstructing the form data and then posting
it to the original page the user was on. This preserves the form data so the
user can continue from where they left off.
This process worked 100% fine in ASP.NET 1.1. It is only once the
application was migrated to 2.0 that the errors began. And they only happen
part of the time and almost seem related to the amount of form data that
needs to be reposted?
Any ideas how to correct or why the errors only occur part of the time etc.
I will probably try turning off eventvalidation to see if that is the reason
since it is difference i can see in the data that is posted back.
(Code for formredirect.aspx - aspx file is blank and the below generates all
of the html).
Public Sub OutputFormData()
Response.Write("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0
Transitional//EN""
""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" + Chr(10))
Response.Write("<html lang=""en"">" + Chr(10))
Response.Write(" <head>" + Chr(10))
Response.Write(" <title>See Spot Reload the Form!!!</title>" +
Chr(10))
Response.Write(" </head>" + Chr(10))
Response.Write(" <body bgcolor=""#ffffff"">" + Chr(10))
Response.Write(" <div id=""showprogress"" style=""DISPLAY:
inline; VISIBILITY: visible; WIDTH: 100%; POSITION: absolute"">" + Chr(10))
Response.Write(" <p align=""center""><img
src=""globalimages/transp.gif"" width=""5"" height=""50""><br>" + Chr(10))
Response.Write(" <font face=""verdana"" size=""4""
color=""#ffcc66""><b>Resubmitting Original Request...Please
Wait...</b></font><br><br>" + Chr(10))
Response.Write(" <img
src=""globalimages/progressbarslower.gif""><br>" + Chr(10))
Response.Write(" <font face=""verdana"" color=""#dddddd""
size=""1"">This may take a few moments...</font><br><br>" + Chr(10))
Response.Write(" <a href=""mainmenu.aspx""><font
face=""verdana"" size=""2"" color=""#ffcc66""><b>Cancel & Return to Main
Menu</b></font></a></p></div>" + Chr(10))
If (Not (Request.Cookies("widsplusformkey") Is Nothing) AndAlso
Request.Cookies("widsplusformkey").Value <> "" AndAlso Not
(System.Web.HttpContext.Current.Cache(Request.Cookies("widsplusformkey").Value) Is Nothing)) Then
Dim rf As Specialized.NameValueCollection =
CType(System.Web.HttpContext.Current.Cache(Request.Cookies("widsplusformkey").Value), Specialized.NameValueCollection)
Response.Flush()
If Not (rf Is Nothing) Then
Response.Write("<form name=""Form1"" method=""post""
action=""" + FormsAuthentication.GetRedirectUrl(User.Identity.Name, False) +
""" id=""Form1"">" + Chr(10))
For i As Integer = 0 To rf.Count - 1
Response.Write("<input type=""hidden"" name=""" +
rf.GetKey(i) + """ id=""" + rf.GetKey(i) + """ value=""" + rf.Item(i) + """>"
+ Chr(10))
Next
Response.Write("</form>" + Chr(10) + "<script
language=javascript>" + Chr(10) + "window.document.forms['Form1'].submit()" +
Chr(10) + "</script>" + Chr(10))
Response.Write(" </body>")
Response.Write("</html>")
' clear the saved post data
System.Web.HttpContext.Current.Cache.Remove(Request.Cookies("widsplusformkey").Value)
Response.Cookies("widsplusformkey").Value = ""
Response.Cookies("widsplusformkey").Path = "/widsplus"
End If
Else
Response.Write(" </body>")
Response.Write("</html>")
Response.Cookies("widsplusformkey").Value = ""
Response.Cookies("widsplusformkey").Path = "/widsplus"
Response.Redirect(FormsAuthentication.GetRedirectUrl(User.Identity.Name,
False))
End If
End Sub
some sporadic viewstate errors...usually saying the viewstate is invalid,
eventvalidation failed or mac error.
My web config does specify a machinekey setting:
<machineKey
validationKey="447C05E8B3A71401CC4CAE5513A7F1A3494A3618EE819316AAD1D58433F236A759D66FB4154500E01EB4E1BC1DE42046E2D652D391CB8367A1649438867A02EB"
decryptionKey="CE8D47C43312A144B49DE5E8D3D3CA2CDEA230077AFB86CB"
validation="SHA1"/>
The errors are occuring during some custom code that i wrote that saves form
data when a user's login times out and restores it by reposting it after the
user logs in.
Specifically, in the global.asax file there is a function as below
(truncated to shorten). When the user's session times out from inactivity,
the entire request.form object (all posted data) is saved in the cache with a
key saved as a cookie on the user's machine.
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires upon attempting to authenticate the user
If (Request.IsAuthenticated) Then
........................
ElseIf (Request.Form.Count > 0 AndAlso
(Request.Cookies("widsplusformkey") Is Nothing OrElse
Request.Cookies("widsplusformkey").Value = "") AndAlso Request.Path.ToLower
<> "/widsplus/login.aspx" AndAlso Request.Path.ToLower <> "login.aspx") Then
Dim guidstring As String = Guid.NewGuid.ToString
' Save any posted form data
System.Web.HttpContext.Current.Cache.Add(guidstring,
Request.Form, Nothing, Now.AddMinutes(16), Cache.NoSlidingExpiration,
Caching.CacheItemPriority.Normal, Nothing)
' save cookie with guid string
Response.Cookies("widsplusformkey").Value = guidstring
Response.Cookies("widsplusformkey").Path = "/widsplus"
Response.Cookies("widsplusformkey").Expires =
Now().AddMinutes(15)
End If
End Sub
Next, on the logon page, once the user is authenticated, it redirects
manually to another page called "formredirect.aspx". The code on that page
(below) custom generates a page reconstructing the form data and then posting
it to the original page the user was on. This preserves the form data so the
user can continue from where they left off.
This process worked 100% fine in ASP.NET 1.1. It is only once the
application was migrated to 2.0 that the errors began. And they only happen
part of the time and almost seem related to the amount of form data that
needs to be reposted?
Any ideas how to correct or why the errors only occur part of the time etc.
I will probably try turning off eventvalidation to see if that is the reason
since it is difference i can see in the data that is posted back.
(Code for formredirect.aspx - aspx file is blank and the below generates all
of the html).
Public Sub OutputFormData()
Response.Write("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0
Transitional//EN""
""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">" + Chr(10))
Response.Write("<html lang=""en"">" + Chr(10))
Response.Write(" <head>" + Chr(10))
Response.Write(" <title>See Spot Reload the Form!!!</title>" +
Chr(10))
Response.Write(" </head>" + Chr(10))
Response.Write(" <body bgcolor=""#ffffff"">" + Chr(10))
Response.Write(" <div id=""showprogress"" style=""DISPLAY:
inline; VISIBILITY: visible; WIDTH: 100%; POSITION: absolute"">" + Chr(10))
Response.Write(" <p align=""center""><img
src=""globalimages/transp.gif"" width=""5"" height=""50""><br>" + Chr(10))
Response.Write(" <font face=""verdana"" size=""4""
color=""#ffcc66""><b>Resubmitting Original Request...Please
Wait...</b></font><br><br>" + Chr(10))
Response.Write(" <img
src=""globalimages/progressbarslower.gif""><br>" + Chr(10))
Response.Write(" <font face=""verdana"" color=""#dddddd""
size=""1"">This may take a few moments...</font><br><br>" + Chr(10))
Response.Write(" <a href=""mainmenu.aspx""><font
face=""verdana"" size=""2"" color=""#ffcc66""><b>Cancel & Return to Main
Menu</b></font></a></p></div>" + Chr(10))
If (Not (Request.Cookies("widsplusformkey") Is Nothing) AndAlso
Request.Cookies("widsplusformkey").Value <> "" AndAlso Not
(System.Web.HttpContext.Current.Cache(Request.Cookies("widsplusformkey").Value) Is Nothing)) Then
Dim rf As Specialized.NameValueCollection =
CType(System.Web.HttpContext.Current.Cache(Request.Cookies("widsplusformkey").Value), Specialized.NameValueCollection)
Response.Flush()
If Not (rf Is Nothing) Then
Response.Write("<form name=""Form1"" method=""post""
action=""" + FormsAuthentication.GetRedirectUrl(User.Identity.Name, False) +
""" id=""Form1"">" + Chr(10))
For i As Integer = 0 To rf.Count - 1
Response.Write("<input type=""hidden"" name=""" +
rf.GetKey(i) + """ id=""" + rf.GetKey(i) + """ value=""" + rf.Item(i) + """>"
+ Chr(10))
Next
Response.Write("</form>" + Chr(10) + "<script
language=javascript>" + Chr(10) + "window.document.forms['Form1'].submit()" +
Chr(10) + "</script>" + Chr(10))
Response.Write(" </body>")
Response.Write("</html>")
' clear the saved post data
System.Web.HttpContext.Current.Cache.Remove(Request.Cookies("widsplusformkey").Value)
Response.Cookies("widsplusformkey").Value = ""
Response.Cookies("widsplusformkey").Path = "/widsplus"
End If
Else
Response.Write(" </body>")
Response.Write("</html>")
Response.Cookies("widsplusformkey").Value = ""
Response.Cookies("widsplusformkey").Path = "/widsplus"
Response.Redirect(FormsAuthentication.GetRedirectUrl(User.Identity.Name,
False))
End If
End Sub