by Daniel
18. June 2008 09:32
Protecting web sites from spam and email harvesting bots is a daily issue when developing web sites for the internet. One easy way to do this is to place the text you want to protect into an image as images can easily be read by humans and are hard to understand by a computer program. There are hundreds of ways to do this. I'd like to show you a simple and flexible way you could go by using XamlWebControls.
Protecting forms via a simple image (also known as CAPTCHA) is quite easy by using the XamlImage control that generates an image with some dynamic text. Below is a simple example that obfuscates the text via rotation, blur, color gradient and some background characters.
You can download the C# source code that demonstrates simple form and email bot protection via XamlWebControls here.
252adfa3-2fb3-4744-b6e8-d6f51d5de696|1|5.0
Tags:
by Daniel
11. June 2008 10:54
Microsoft's LinQ - in any shape - is in my opinion one of the most impressive and useful technology stacks that Microsoft has released since .NET itself. I loved it from the very first day and it has saved me ours of time writing unspectacular lines of iterations, loops, dump SQL code and Database Adapters. Since Microsoft implemented LinQ as a language feature it is of course type safe and in many szenarios very good this way. But when it becomes a little bit more dynamic, the initially strongly typed nature of a LinQ statement is a little bit of a barrier. Sometimes I need really dynamic queries that are a result of several unpredictable parameters defined at runtime. That's where "dynamic link" comes into play. Scott blogged about this "LINQ Dynamic Query Library" at the beginning of this year. Very nice!
But to be honest - after using it several times - it seems to me like a forgotten feature that only few care about. Besides the fact that it's basically just a simple class that triggers some runtime compilation - it's quite poorly documented and slightly uncomplete. Since it's untyped, accessing the resulting data can become quite unhandy. I wrote some additional Extension Methods that I initially missed and now I use them quite frequently and I'd like to share with you.
The first extension method I was really looking for was the "distinct" feature of LinQ. It's not that special - but when you are in a hurry and you have no experience in writing these extension methods it may become quite useful:
/// <summary>
/// Distincts the specified source.
/// </summary>
/// <param name="source">The source.</param>
/// <returns></returns>
public static IQueryable Distinct( this IQueryable source )
{
if( source == null )throw new ArgumentNullException( "source" );
return source.Provider.CreateQuery(
Expression.Call( typeof( Queryable ), "Distinct", new Type[] { source.ElementType }, source.Expression ) );
}
The next extension method I added is one to provide the resulting data as a simple typed List. It makes sense only for one dimensional result sets of course:
/// <summary>
/// Converts the first column of the specified source into a List of type T.
/// </summary>
/// <param name="source">The source.</param>
/// <returns></returns>
public static List<T> ToList<T>( this IQueryable source )
{
List<T> r_List = new List<T>();
PropertyInfo l_FirstProperty = null;
IEnumerator l_Enumerator = source.GetEnumerator();
while( l_Enumerator.MoveNext() )
{
object l_CurrentObject = l_Enumerator.Current;
if( l_FirstProperty == null )
{
PropertyInfo[] l_PropertyInfoCollection = l_CurrentObject.GetType().GetProperties();
l_FirstProperty = l_PropertyInfoCollection[0];
}
l_List.Add( (T) l_FirstProperty.GetValue( l_CurrentObject, null ) );
}
return r_List;
}
Currently I am writing Extension Methods that can return result sets as a DataTable or multi dimensional list from a dynamic LinQ query. If you are interested in them, don't hesitate to contact me...
c8fe53b8-c695-43b6-823d-702a04b635a1|1|5.0
Tags: linq