Weird Error : Unable to Validate Data in ASP.NET website

Have you ever come across a situation where your website which was working for last couple of months gives a weird error "Unable to Validate Data". Yesterday, while working I found that my website which is already published in IIS throws this error. Initially I thought the error might be with my code, but I found that everything is good with the code. So I looked forward to internet and found that this error comes when the viewstate of a page cannot be decrypted when the response received from the client.

When I look where the error is occurring(Target Site) I found:

Unable to validate data at
System.Web.Configuration.MachineKey.GetDecodedData(Byte[] buf, Byte[] modifier,
Int32 start, Int32 length, Int32& dataLength) at
System.Web.UI.LosFormatter.Deserialize(String input)

Actually the problem is with the viewstate. The viewstate is actually decrypted in the server using a secret Machine key which resides on the server. The interesting thing is the key gets regenerated after a certain time. Therefore when the user returns the viewstate, if the machine identified key is changed, the decryption of viewstate fails and thus throws this nasty error.

Solution

The solution is simple. First of all, to solve the issue, I disabled the ViewState for the current page by putting EnableViewState = false. Even I have disabled this for the entire viewstate for the website using Web.config. But still the error.
Finally I have used "EnableViewStateMac =false" in pages section. Voila, this cures the problem.
<pages buffer="true" enableViewStateMac="flase"/>
</pages>
Just place the following between the system.web section and the site starts working.

Another solution that you might use as well is to place the machine key directly on your web.config, so that it always decrypts and encrypts using the static key values. To do this you need to use the following :

<machinekey validationkey="22E995276703B846C4AD0881EE3159FFCB376CD48B27F64
9A074815C83D5C925038938F6943555687582CBC186DB22E552FCDB4D46
124BAADEE85A857CC135BC" decryptionkey="60588EB661A6483348C20F92659775872CB06427AF20733C" validation="SHA1"></machinekey>

You might use this site to Generate your validation key as well.
To get the deep knowledge on what makes this happen, I found some insight from Internet and reading some articles of msdn. Let us talk a little on that note.
Say you made a request for a page in the server. After you place the request the server processes it, encrypts the viewstate that the server receives using the encryption mentioned. Basically it uses the key mentioned in the Machine.config to encrypt the viewstate data. Finally it converts to Base64 and embed into some hidden fields.

We can mention the machine key in Web.config too so that it uses it for the current website. You might use AutoGenerate option too to enable/disable autogeneration of key during the runtime.

Your comments are welcome.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Grant access to your Assembly from COM objects

This is a very common occasion where we need to expose a .NET assembly to COM applications, so that the COM application can communicate with .NET assemblies. Here I am going to create a .NET Assembly and expose it from COM.

Your COM Exposed .NET class :

Let us create the Class which I need to expose to COM.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ComVisibleObject
{
[Guid("EB7F73B0-AF1F-4595-8CE8-849D3745E862")]
public interface IComVisibleObject
{
string GetString();
void SetString(string value);
}

[Guid("DF8B1227-68EC-4F76-8C79-D20574C21B56")]
[ComVisible(true)]
public class ComVisibleObjectEx : IComVisibleObject
{
private string element = string.Empty;

public string GetString()
{
return element;
}

public void SetString(string value)
{
element = value;
}

}
}

Here I have created an Interface called IComVisibleObject which might be used to create the class. Please note, it is not mandatory to create the interface.

To Uniquely Identify each class we placed GuidAttribute to each of them. The class ComVisibleObjectEx is exposed to COM using ComVisibleAttribute set to true.

After your class is created in Class Library, Right click on Project - > Go to Properties and select Register for COM interop.
Again Under the signing Tab Select Sign the Assembly. In the Choose key file combobox select New. Choose Filename, Username and password.. The Strong name key will be created automatically.

Now Build the project. Say the dll produced is ComVisibleObject.dll. Use GacUtil to put it in Global Assembly Cache. Use this :
gacutil -i ComVisibleObject.dll

If its successful it will confirm "Assembly successfully added to the cache”.

To add it from COM applications, you need to register assembly using regasm tool.Use

regasm ComVisibleObject.dll
After registration is successful, it will say "‘Types registered successfully".

Now let us create your COM application. For simplicity I use vbs. You can easily create an original VB application to test this as well

Dim object
set object = CreateObject(”ComVisibleObject.ComVisibleObjectEx”)
MsgBox(”Created the object.”)
defaultText = object.GetString()
MsgBox(”Default text length : ” & Len(defaultText))
object.SetString(”My new string”)
newText = object.GetString()
MsgBox(”New text is ” &amp; newText)

Finally save the file as Example.vbs.
Open Command prompt and type

cscript Example.vbs

You will see the messageboxes show the text.

Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

UnCommon C# keywords - A Look

 This is really a weird topic to start with. But still I would like to give you an insight on some of the uncommon things that you may not have noticed while doing programming. I have framed them in 2 sections.

1st one for Undocumented Keywords, which you will not find anywhere, not even in MSDN Documentation, which are not listed to intellesense menu in visual studio.
2nd one for Documented Keywords which are uncommon or just being introduced to C#. Documented keywords which are uncommon can be found over MSDN.I have also made a sample application where I have given some demonstration of each of the topics mentioned here in this article. If you want to test these, please download the sample application from here :



Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Compress Response

It is obvious that we need to compress our response while passing to the client. Compress requests and response are always increases performance of sites. Nowadays, almost 99 percent of the browsers supports either Gzip or Deflate compression or both. So if we can check if accept-encoding header is present and return compressed response to the client, then we have just improved our site performance. Just take a look on the code below :

public static void doCompression()
{
HttpContext context = HttpContext.Current;
HttpRequest request = context.Request;
string acceptEncoding = request.Headers["Accept-Encoding"];
HttpResponse response = context.Response;
if (!string.IsNullOrEmpty(acceptEncoding))
{
acceptEncoding = acceptEncoding.ToUpperInvariant();
if (acceptEncoding.Contains("GZIP"))
{
response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
response.AppendHeader("Content-encoding", "gzip");
}
else if (acceptEncoding.Contains("DEFLATE"))
{
response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
response.AppendHeader("Content-encoding", "deflate");
}
}
response.Cache.VaryByHeaders["Accept-Encoding"] = true;
}


Here we are checking if the Request header contains [“Accept-Encoding”]. Based on the encoding it supports we filter the response using GZipStream / DeflateStream which are available in System.IO.Compression namespace. Thus the response will be compressed.
Only this will not help the client to render your page properly. You need to add the Content-Encoding header to the response, so it can decompress the response in the client properly. We add Response header “Accept-Encoding” so that request is also made from now using the same encoding it is sending the data.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Linq Basics

Hi Folks,
Hope you all are enjoying this blog. Its a long time since I last posted in this blog. As technology is getting richer and richer, I must write something new in this post. Right now it is definitely a phase of all of us to take the new .NET framework 3.5 with having lots of flexibility in both coding as well as technology.
Lets start with Whats New in 3.5?

You know the three concepts that was added with .NET 3.0 :
1. Windows Communication Foundation (.NETs first attempt to merge all the existing remoting concepts into a single Service Oriented Architecture).
2. Windows Presentation Foundation (Improvement in presentation layer so that user interface could be enhanced very easily.
3. Workflow foundation ( Easily create workflows to generate business logics, Managing object lifecycles / persistent object storage )

After that after the introduction of .NET 3.5 language enhancements are also made so that programmers could make use of technology more easily to enhance their code easily. The recent changes to C#.NET are :
1. LINQ ( Language Integrated Query)
2. Implicitly Typed Interface
3. Object and Collection Initializers.
4. Extension methods
5. Anonymous Types
6. Lamda Expressions
7. Auto implemented properties.

LINQ
Linq is the Microsoft's first attempt to integrate queries into language. We know, it is really easy to find data from sql objects simply writing a query while its somewhat hectic when we want to do the same thing in a DataTable or Lists. Generally we will have to loop through every elements to find the exact match, if there is some aggregation we need to aggregate the values etc. Linq provides an easy way to write queries that can run with the in memory objects. Let us demonstrate that :

Suppose we want to find all the employees whose age is above 50. From database if we want to do this we would write :
select * from employees where age > 50
In case of doing this from code, let us suppose we have a list of employees which have a property age in it. Now before linq we will write :
List<employee> filteredList = new List<employee>();
foreach(Employee emp in employees) // where employees is a list
{
If(emp.Age > 50)
filteredList.add(emp)
}


If we use linq we would write like :
List<employee> filteredList = new List<employee>();
var filterEnumerable = from emp in employees
where emp.Age >50 select emp;
filteredList = filterEnumerable.ToList();


Thus we see how easy to write a linq expression. Let us demonstrate it a bit more.

From emp in employees : This is the temporary elements for each query loop. By from emp we mean we are creating a var of emp and set the objects that comes from employees list one after another.
where emp.Age >50 select emp: means we are imposing restriction on the emp, so that if its age is more than 50 then select emp.

Lambda Expressions:
C# also provides lamda expressions to have short hand writing of linq expressions using Extension methods to static Enumerable Class. This extension methods are added to every Enumerable objects.
We can write the same filterList Linq Expression using Lamda Expression like this :
var filterEnumerable = employees.Where<employee>(emp => emp.age > 50);
For every linq expression there is a corresponding Lambda expression.

What is VAR?
Var is implicitly typed Interface which comes very handy in case of anonymous types. Suppose we write like:
var customers = from c in customersjoin o in orders on c.CustomerIDequals o.CustomerID into cofrom o in co.DefaultIfEmpty(emptyOrder)select new { c.Name, o.OrderDate, o.Total };
This returns a new type object to an enumerable lists.

Every linq statements returns an IEnumerable list of objects. You may now think what var signifies. Actually it is a implicitely typed interface. It assigns its type based on the object it finds. Thus if we set var i= 10 it will signify that variable i is an Integer variable.

Now let us delve more into Linq with examples, Linq comes with lots of Operators:
  • Restriction operators
  • Projection operators
  • Partitioning operators
  • Join operators
  • Concatenation operator
  • Ordering operators
  • Grouping operators
  • Set operators
  • Conversion operators
  • Equality operator
  • Element operators
  • Generation operators
  • Quantifiers
  • Aggregate operators
Now Let us take each of them one by one.
First I have created three lists :
List employees = new List();
List employees1 = new List();
List orders = new List();

employees.Add(new Employee { age = 40, name = "Basob" });
employees.Add(new Employee { age = 34, name = "Abhishek" });
employees.Add(new Employee { age = 65, name = "Souvik" });
employees.Add(new Employee { age = 65, name = "Ayan" });
employees.Add(new Employee { age = 68, name = "Raj" });
employees1.Add(new Employee { age = 68, name = "Pallab" });
employees1.Add(new Employee { age = 55, name = "Swarup" });
employees1.Add(new Employee { age = 68, name = "Ranjit" });
employees1.Add(new Employee { age = 68, name = "Bratin" });
orders.Add(new Order { empName = "Raj", itemName = "Pen" });
orders.Add(new Order { empName = "Souvik", itemName = "Pencil" });
orders.Add(new Order { empName = "Raj", itemName = "Rubber" });
Now Let us use these lists to demonstrate each of the operators.

Restriction Operator:
Restriction operator can be applied by using Where clause. Example of Where clause:
var filterEnumerable = from emp in employeeswhere emp.age > 50select emp;ORvar filterEnumerable = employees.Where<employee>(emp => emp.age > 50);

This filters out Employees by age greater than 50.

Projection Operator:
With the word projection, I mean Select statements. Every linq elements should have projection in it.
var iNames = from i in employees select i.name;ORvar iNames = employees.select<employee,string>
Here IEnumerable of Name is returned.

Partitioning using Take /Skip operators
Take can be used when we take first N elements in a list, skip will take the elements after N.
var MostAged2 = employees.OrderByDescending(i =>i.age).Take(2);
var AllButMostAged2 =employees.OrderByDescending(i => i.age).Skip(2);

Takewhile and skipwhile operator will select from a list based on a delegate passed in.

var allWithfourwordlength = employees.SkipWhile<employee>(r => r.name.Length > 4);


Join Operators:
Join operators have 2 parts. The outer part gets results from inner part and vice versa so returns the result based on both
var filterEnumerable = from emp in employeesjoin ord in orders on new { Name = emp.name }equals new { Name = ord.empName }select emp;ORvar filterEnumerable = employees.Join<employee,Order, string, Employee>(orders, e1 => e1.name,o => o.empName, (o, e2) => o);
Here we are joining employees and order based on the names passed in

Concatenation Operator :
The Concatenation operator concats two sequence.
var items = ( from itEnt in _itemListwhere itEnt.Category.Equals("Entertainment")select itEnt.ItemName).Concat(from it2 in _itemListwhere it2.Category.Equals("Food")select it2.ItemName).Distinct();
This will concat categories of Entertainment and Food. Distinct oprator can also be used to evaluate only distinct elements in resultset.
OrderBy / ThenBy
Orderby/ThenBy can be used to order dataresults.

var orderItems = from emp in employees orderby emp.name,emp.age descending;
var orderItems =employees.OrderBy(i => i.name).ThenByDescending(i => i.age);
Here the ordering is done by name and then decending by age.

GroupBy Operator :

This is used to group elements.
var itemNamesByCategory =from i in _itemListgroup i by i.Category into gselect new { Category = g.Key, Items = g };
This gets all the categories and items grouped by category. Well this grouping seems to be a little tricky for me. Let me make you understand what exactly is the way. Here while we are grouping, we are taking the group into g(which is a IGrouping). The g will have a key, which holds the grouped data, you can add multiple grouping statement. If you want to have a having clause, its just the where clause will do. Just like the example below:
var filterEnumerable2 = from emp in employees
where emp.age >65 //Normal Where clause works on all items
group emp by emp.age into gr
where gr.Key > 40 // Grouped where clause similar to Having Clause
select new { aaa = gr.Key, ccc=gr.Count(), ddd=gr.Sum(r=>r.age), bbb = gr };
Here in the example, I have returned the aggregate functions like sum, count etc which you can find from group data.

Distinct / Union / Intersect / Except Operators :

Union operator produces an union of two sequences
var un = (from i in _itemListselect i.ItemName).Distinct().Union((from o in _orderListselect o.OrderName).Distinct());
Intersect operator produces an intersection of two sequences.
var inter = (from i in _itemListselect i.ItemID).Distinct().Intersect((from o in _orderListselect o.OrderID).Distinct());
Except operator produces a set of difference elements from two sequences.
var inter = (from i in _itemListselect i.ItemID).Distinct().Except((from o in _orderListselect o.OrderID).Distinct())

Well there are lots more to tell you about LINQ. I am stopping it here. If you want more,
Read the Full Article.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!