Thursday, July 31, 2008

Scheduling Backups for SQL Server 2005 Express

"One problem with SQL Server 2005 Express is that it does not offer a way to schedule jobs. In a previous tip, Free Job Scheduling Tool for SQL Server Express and MSDE, we looked at a free tool that allows you to create scheduled jobs for SQL Server. The one issue people often face though is what to install and what not to install on their production servers and therefore these items go without resolution. One very important part of managing SQL Server is to ensure you run backups on a set schedule. I often hear about corrupt databases and no backups, so let's take a look at another approach of scheduling backups using the included tools in both the operating system and SQL Server."
 
This article provides an easy solution for using the SQL BACKUP command to backup a database and shows you how to use SQLCMD.EXE with Scheduled Tasks to schedule it to run automatically, thus giving you the scheduled backups functionality you would typically have in SQL Server Agent.
 

Web Farms and ASP.NET ViewState

"The default ASP.NET settings ensure that forms authentication tickets are tamper proof and encrypted, and that ViewState is tamper proof. This ensures that any modification of the ViewState or authentication tickets either on the client's computer or over the network is detected when the server processes the data."
 
For example, when the viewstate generated on server A is posted back to server B you may receive a viewstate validation error if the <machineKey> is not the same on all of the servers in the web farm or cluster.
 
Here is one of the exceptions you may receive:
 
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
 
This is because the viewstate is salted with a unique, autogenerated machine key from the originating server's machine.config file.
 
"This is done to prevent users from somehow tampering with the ViewState. Any change to the ViewState data on the client will be detected. But this has a side effect: it also prevents multiple servers from processing the same ViewState. One solution is to force every server in your farm to use the same key -- generate a hex encoded 64-bit or 128-bit <machineKey> and put that in each server's machine.config"
 
If you do not have access to the machine.config files in the web farm or cluster you can disable the enableViewStateMac using a simple page directive:
 
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyPage.aspx.vb"
 Inherits="MyAssembly.MyPage" enableViewStateMac="false" %>
 
Alternately, you can modify the pages element in Web.config:
 
<system.web>
  <pages enableViewStateMac="false" />
</system.web>

Of course caution should be taken when setting the enableViewStateMac to false since the viewstate could potentially be tampered with.
 
If you have a web farm or cluster and receive this error the optimal solution would be to update the machine.config files on all of the servers in the web farm or cluster to ensure that the <machineKey> configuration specifies the same validationKey and validation algorithm.