0

net applications webapp1 and webapp2, in each application i have a asp.net form Deafult.aspx

I want to do a form submit from Default.aspx in webapp1 and recieve the value in webapp2.

I tried to do it with simply setting action ="webapp2 location" but it is throwing the bellow error

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.

I even added the machinekey element to web.config

but it is still showing the same error.

This is the code for webapp1 form which sends data to webapp2

<form id="form1" method="post" runat="server" action="http://localhost/webValRec/Default.aspx">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Value to pass "></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>

The code for the form that recieves values and displays in label

<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>

the code behind to set the label value

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(Request.Form["TextBox1"]);
    }
}

Thank you

2 Answers 2

2

One quick way to get around the error is to simply not submit the viewstate to the second page. You could remove it via javascript during the onload event of webapp1/Default.aspx

Add a javascript function that will be called onload...

// javascript function to remove viewstate form element.
function removeViewState() {
    var viewstate = document.getElementById('__VIEWSTATE');
    viewstate.parentNode.removeChild(viewstate);
}

Add the onload event to the body to call the function...

<body onload="javascript:removeViewState();">

If you are using jquery, you could make it even easier by removing the viewstate with the document ready handler...

$(document).ready(function () {
    $("#__VIEWSTATE").remove();
});
Sign up to request clarification or add additional context in comments.

Comments

2

You hits the bull's eye : you need to ensure view state validation on separate servers.

Solutions are :

  • Disable ViewState : EnableViewState="false"
  • Disable MAC validation ('unsecure' solution) : EnableViewStateMac="false"
  • Configure the two servers : How To: Configure MachineKey in ASP.NET 2.0. You may have missed the encryption & decryption key generation part ?

The forms may have to be strictly the same too (same controls) when processing a postback : add missing controls on your webapp2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.