Even with ASP.NET 3.5, I am getting "Validation of viewstate MAC failed" error - asp.net-3.5

I found a lost of posts to solve this error: "Validation of viewstate MAC failed". I read that it is a ASP.NET 2.0 error which is also a bug. But I am using VS 2008 with ASP.NET 3.5 SP1. Why this error is coming in this version also?
I am using ASP TextBox controls. Some posts have mentioned that ASP TextBoxes generate this error, so I have set AutoPostBack of each TextBox to False.
How to get rid of this mess?
Code Added Below **
* Default.aspx *
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FlexStock._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<!--<%# Register Assembly="MSCaptcha" Namespace="MSCaptcha" TagPrefix="cc1" %>-->
<link rel="Stylesheet" type="text/css" href="~/Styles/main.css" />
<title>FlexStock#.NET - Gamma Edition</title>
</head>
<body id="LoginPageBody" class="loginPageBodyClass">
<img id="imgProductLogo" src="Styles/stockist_gamma.png" style="text-align:center;text-shadow:black;margin-left:500px" alt="Product Logo"/>
<form id="loginForm" runat="server" class="loginForm" action="./Forms/selectCompany.aspx" >
<div id="divMainLoginPage" runat="server">
<div class="label">
<asp:Label ID="lblUserName" runat="server" CssClass="LabelControls" AssociatedControlID="txtUserName">User Name: </asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtUserName" runat="server" CssClass="TxtStyle" Width="199px" AutoPostBack="false"></asp:TextBox>
</div>
<div class="label">
<asp:Label ID="lblPassword" runat="server" CssClass="LabelControls" AssociatedControlID="txtPassword">Password: </asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtPassword" runat="server" CssClass="TxtStyle" TextMode="Password" Width="199px" AutoPostBack="false"></asp:TextBox>
</div>
<div class="label">
<asp:Label ID="lblCaptcha" runat="server" Text="Security Check"></asp:Label>
</div>
<div class="value">
<!-- <cc1:CaptchaControl ID="ccJoin" runat="server" CaptchaBackgroundNoise="none" CaptchaLength="5" CaptchaHeight="60" CaptchaWidth="200" CaptchaLineNoise="None" CaptchaMinTimeout="5" CaptchaMaxTimeout="240" /> -->
</div>
<div class="label">
<asp:Label ID="lblEnterCaptcha" runat="server" Text="Enter Value"></asp:Label>
</div>
<div class="value">
<asp:TextBox ID="txtCaptcha" runat="server" CssClass="TxtStyle" Width="199px" AutoPostBack="false"></asp:TextBox><br /><br />
<asp:Button ID="btnLogin" runat="server" Width="60" Text="Login" CssClass="BtnStyle"/>
</div>
</div>
</form>
<img id="img1" src="Styles/impact_logo.png" style="text-align:right;text-shadow:black;margin-left:800px" />
</body>
</html>
selectCompany.aspx **
<%# Page Title="" Language="C#" MasterPageFile="~/master1.Master" AutoEventWireup="true" CodeBehind="~/Forms/selectCompany.aspx.cs" Inherits="FlexStock.Forms.selectCompany" EnableViewStateMac="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link rel="Stylesheet" type="text/css" href="../Styles/selectCompany.css" />
<link rel="Stylesheet" type="text/css" href="../Styles/main.css" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="frmSelectCompany" enableviewstate="false">
<div id="label" class="label" style="width:300px">
<asp:Label ID="lblSelectCompany" runat="server" Text="Select Company or Create New"></asp:Label>
</div>
<div id="btnCreate" style="text-align:right">
<asp:Button ID="btnCreateNew" runat="server" Text="Create New" CssClass="BtnStyle" />
</div><br />
<div id="divGrid">
</div>
</form>
</asp:Content>

This is a pretty common error to see. It's a security feature in ASP.NET that prevents someone from injecting additional controls onto the page after it has rendered, and posting those controls back to the server. It makes your website harder to hack.
Are you creating any input controls through javascript? If so, don't. Create the controls normally inside the web form and wrap them in a <div style='display:none;'> and display them when you need them.
Are you creating any controls dynamically within C#? - If you are, you must create them inside Page_Init, not Page_Load, otherwise ASP.NET won't recognise them as valid.
You can disable this security measure by setting EnableViewStateMac=False at the top of the aspx page, but I strongly advise against it.

Related

expression language in jstl if statement not working

I am trying to understand why this piece of code never prints the method request when run on tomcat 9:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>Add Course</title>
</head>
<body>
<c:if test="${\"POST\".equalsIgnoreCase(pageContext.request.method) && pageContext.request.getParameter(\"submit\") != null}">
<%= request.getMethod() %>
</c:if>
<form method="post">
Name: <input type="text" name="name"> <br>
Credits : <input type="text" name="credits"> <br>
<button type="submit" name="submit">Add</button>
</form>
</body>
</html>
I don't have Tomcat 9 currently installed on my system, but it runs fine in Tomcat 8.5, and Jetty 9.4.17 printing "POST" whenever I post the form.
However, el language, despite resembling Java in many aspects and being able to call Java functions is NOT actually Java. You will encounter more success and consistency using it in a more idiomatic way. In your case, I'm pretty sure the following will work on any up-to-date JSP server:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<title>Add Course</title>
</head>
<body>
<c:if test="${'post' eq fn:toLowerCase(pageContext.request.method) and param.submit ne null}">
${pageContext.request.method}
</c:if>
<form method="post">
Name: <input type="text" name="name" /><br />
Credits: <input type="text" name="credits" /><br />
<button type="submit" name="submit">Add</button>
</form>
</body>
</html>
Using the fn taglib to compare strings in lower case
Using single quotes around strings to avoid escaping
Using el eq and ne operators for "equals" and "not equals"
Using el and boolean operator
Using el variable param to access request parameters
Using ${...} syntax to output values rather than scriptlets

How to include properly a css file in jsp

I tried this :
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" />
but the css won't apply. :(
Am i missing something obvious ?
EDIT :
Here is my jsp :
<%# page pageEncoding="utf-8"%>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>Log-in</title>
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css" />
</head>
<body>
<div class="login-card">
<h1>Log-in</h1><br>
<form>
<input type="text" name="user" placeholder="Username">
<input type="password" name="pass" placeholder="Password">
<input type="submit" name="login" class="login login-submit" value="login">
</form>
<div class="login-help">
Register • Forgot Password
</div>
</div>
</body>
</html>
Try this:- <jsp:include page="style.css"/>
Using the include directive should do the trick.
<link type="text/css" rel="stylesheet" href="css/style.css"/>
try this, will work. In java it ma

schema.org markup and nesting for Review of "ApartmentComplex"

I have some structured data implemented in my website using microdata so that Google and other search engines could parse it and show appropriate rich snippets. I have added the appropriate markup for all the microdata tags that I have used but I am unable to view the rich snippets for my website while testing it using the Rich Snippets Testing Tool. I have gone through the usage guidelines and frequent issues section at the Google Webmaster but to no avail.
Upon debugging the html I found that the following snippet was successfully showing rich snippets when fed to the Rich Snippets Testing Tool.
<div class="row">
<div class="col-sm-12">
<div class="page-header">
<h1><span itemprop="name">T Park </span></h1>
<div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress" class="address">
<span itemprop="streetAddress">1 Scenic Park</span>
<span itemprop="postalCode" class="hidden">123456</span>
<span itemprop="addressRegion" class="hidden">Central </span>
<span itemprop="addressCountry" class="hidden">Singapore</span>
</div>
</div>
</div>
</div>
<div class="col-sm-6 box-map">
<h2>Location</h2>
</div>
<div style="margin-bottom:0px;" itemprop="review" itemscope itemtype="http://schema.org/Review" class="jumbotron row">
<h2>T Park Reviews</h2>
<br>
<meta itemprop="itemReviewed" content="T Park">
<div id="reviews" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
<meta itemprop="worstRating" content="1">
<meta itemprop="ratingValue" content="9">
<meta itemprop="bestRating" content="10"><span>Rating: 9/10</span>
</div>
<br><span itemprop="reviewBody">Lorem Ipsum....</span>
<br><strong itemprop="author" class="row pull-right">John May</strong>
</div>
However as soon as an enclosing markup of ApartmentComplex is added as shown in the snippet below, the rich snippet is not visible.
<div itemscope itemtype="http://schema.org/ApartmentComplex" class="container">
<div class="row">
<div class="col-sm-12">
<div class="page-header">
<h1><span itemprop="name">T Park </span></h1>
<div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress" class="address">
<span itemprop="streetAddress">1 Scenic Park</span>
<span itemprop="postalCode" class="hidden">123456</span>
<span itemprop="addressRegion" class="hidden">Central</span>
<span itemprop="addressCountry" class="hidden">Singapore</span>
</div>
</div>
</div>
</div>
<div style="margin-bottom:0px;" itemprop="review" itemscope itemtype="http://schema.org/Review" class="jumbotron row">
<h2>T Park Reviews</h2>
<br>
<meta itemprop="itemReviewed" content="T Park">
<div id="reviews" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
<meta itemprop="worstRating" content="1">
<meta itemprop="ratingValue" content="9">
<meta itemprop="bestRating" content="10"><span>Rating: 9/10</span>
</div>
<br><span itemprop="reviewBody">Lorem Ipsum....</span>
<br><strong itemprop="author" class="row pull-right">John May</strong>
</div>
</div>
So assuming my page is about ApartmentComplex, and I want to include a Review of it, how should I structure/nest this markup?
I don't think there's anything wrong with your mark-up or Microdata. I just don't think that the Google Structured Data tool shows any rich snippets for http://schema.org/ApartmentComplex types - rich snippets are only available for certain schema.org types.
To prove this, change the wrapping ApartmentComplex type to a Product type, and remove the address (which isn't part of "Product") and you'll see that a rich snippet is produced in the Google Structured Data Testing Tool (because Google do show rich snippets for Products).
Edit to add: this is a possible workaround - Google will show rich snippets for schema.org types where the surrounding type is a Review, so you could use Review as the top-level type, then have the ApartmentComplex type as the "itemReviewed" property - this works in the Google Structured Data Testing Tool:
<div itemscope itemtype="http://schema.org/Review" class="container">
<div class="row" itemprop="itemReviewed" itemscope itemtype="http://schema.org/ApartmentComplex">
<div class="col-sm-12">
<div class="page-header">
<h1><span itemprop="name">T Park </span></h1>
<div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress" class="address">
<span itemprop="streetAddress">1 Scenic Park</span>
<span itemprop="postalCode" class="hidden">123456</span>
<span itemprop="addressRegion" class="hidden">Central</span>
<span itemprop="addressCountry" class="hidden">Singapore</span>
</div>
</div>
</div>
</div>
<div style="margin-bottom:0px;" class="jumbotron row">
<h2>T Park Reviews</h2>
<br>
<div id="reviews" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
<meta itemprop="worstRating" content="1">
<meta itemprop="ratingValue" content="9">
<meta itemprop="bestRating" content="10"><span>Rating: 9/10</span>
</div>
<br><span itemprop="reviewBody">Lorem Ipsum....</span>
<br><strong itemprop="author" class="row pull-right">John May</strong>
</div>
</div>

Uncaught reference error lift ajax is not defined

I'm trying to create a Lift chat server. I've taken everything straight from the Lift book that is linked to from Lift's main website. Upon running it, I cannot submit my messages because liftAjax is undefined and causes an error that appears in my chrome console.
Correct me if I'm wrong, but shouldn't Lift generate the liftAjax stuff upon starting up the website? I have a feeling I could import liftAjax myself from some source and it would work, but I don't feel I should have to do this.
This is my index.html file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Home</title></head>
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
<!-- the behavior of the div -->
<div class="lift:comet?type=Chat">Some chat messages
<ul>
<li>A message</li>
<li class="clearable">Another message</li>
<li class="clearable">A third message</li>
</ul>
</div>
<div>
<form class="lift:form.ajax">
<input class="lift:ChatIn" id="chat_in" />
<input type="submit" value="Say Something" />
</form>
</div>
</div>
</body>
</html>
This is what is generated:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div id="main" xmlns="http://www.w3.org/1999/xhtml">
<!-- the behavior of the div -->
<div id="F156429460218VSI1GX_outer" style="display: inline">
<div id="F156429460218VSI1GX" style="display: inline">
<div xmlns="http://www.w3.org/1999/xhtml">Some chat messages
<ul>
<li></li>
</ul>
</div><script type="text/javascript">
// <![CDATA[
/* JSON Func comet $$ F156429460221QEXVXK */function F156429460221QEXVXK(obj) {liftAjax.lift_ajaxHandler('F156429460221QEXVXK='+ encodeURIComponent(JSON.stringify(obj)), null,null);}
// ]]>
</script></div><script type="text/javascript">
// <![CDATA[
var destroy_F156429460218VSI1GX = function() {}
// ]]>
</script></div>
<div>
<form id="F156429460223BHVYIM" action="javascript://" onsubmit="liftAjax.lift_ajaxHandler(jQuery('#'+"F156429460223BHVYIM").serialize(), null, null, "javascript");return false;">
Note: the error happens on the above line
<input name="F1564294602245ECYAU" id="chat_in" xmlns="http://www.w3.org/1999/xhtml" />
<input value="Say Something" type="submit" xmlns="http://www.w3.org/1999/xhtml" />
</form>
</div>
</div>
I'm just not really sure what could be causing this. I'm just starting to learn Lift so I do not know much. Am I missing something?
If you need more information, such as build.sbt or other files just let me know and I can post them as well.
In my case, I was missing following body part that automatically seems to source liftAjax.
<body class="lift:content_id=main">
<div id="main" class="lift:surround?with=default;at=content">
......
</div>
</body>
But, in another case, sourcing jquery manually would work with JQuery module injected in Boot.scala.
<script id="jquery" src="/classpath/jquery.js" type="text/javascript"></script>
You don't need to manually include the liftAjax function, but you do need to include jquery manually. Lift will not automatically inject a reference to it since it doesn't know what version, minification level, or location you want to use. Try adding:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
To the head section of your template.

ASP.NET MVC2 DataAnnotaion Validation within an updatepanel

I am currently working on a website using MVC2 and using ASP.NET Ajax to handle moving between pages. Everything is working fine except a page that has a form for the user to fill out that uses DataAnnotations for validation. This form falls within my UpdatePanel and won't conduct server or client side validation, both which I have working if I remove the UpdatePanel.
Is this even possible, or am I missing a step?
Here is a general outline of my code:
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcValidation.js" type="text/javascript"></script>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptMgr" runat="server" ScriptMode="Release">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<div class="page">
<div id="main">
<% Html.EnableClientValidation(); %>
<h2>
ContactUs</h2>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Take a look at Ajax.BeginForm instead of using the update panel.