loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

ReCaptcha - Getting the Challenge Key and/or image with Asp.Net MVC

by naspinski 8/1/2011 11:30:00 AM

Often times you do not need the whole deal, or you are working with a technology that doesn't employ javascript or iframes

Recently I was working on a project that needed to provide (via MVC 3) the ReCaptcha image and/or challenge key only, and not the whole html/javascript portion due to limitations by the data consumer. It took me a while, but there is quite an elegant and simple solution to this.

Here is the controller I came up with:
public class CaptchaController : Controller
{
    public string contentReturnType = "text/plain";
    public string googleChallengeUrl = "http://
        www.google.com/recaptcha/api/challenge?k={0}";
    public string googleImageUrl = "http://
        www.google.com/recaptcha/api/image?c={0}";
    public string googleImageHtml = "<img style=
        \"display: block; \" height=\"57\" width=\"300\" 
         src=\"{0}\"/>";

    [HttpGet]
    public ContentResult GetChallengeKey(
        string proxyIpAndPort = null)
    {
        string http = string.Format(googleChallengeUrl, 
            ReCaptchaValues.PublicKey);
        WebClient client = new WebClient();

        //assuming no proxy on a local machine
        if (!string.IsNullOrEmpty(proxyIpAndPort) && 
            !Request.IsLocal) 
        {
            client.Proxy = 
                new WebProxy(proxyIpAndPort, true);
        }

        string html = client.DownloadString(http);
        int start = html.IndexOf('{');
        int length = html.IndexOf('}') - start + 1;
        string json = html.Substring(start, length);

        ReCaptchaState state = 
            new JavaScriptSerializer()
                .Deserialize(json);
        return this.Content(state.challenge, 
            contentReturnType);
    }

    [HttpGet]
    public ContentResult GetImageUrl()
    {
        return this.Content(string.Format(googleImageUrl, 
            GetChallengeKey().Content), contentReturnType);
    }

    [HttpGet]
    public ContentResult GetImageHtml()
    {
        return this.Content(string.Format(googleImageHtml, 
            GetImageUrl().Content), contentReturnType);
    }
}

This is using json deserializing of the ReCaptcha return string into a .Net object named ReCaptchaState which is covered here.

Now all that you need to do is call one of the urls to get the desired result in a plaintext format (you can change the output if you wish).

CohesiveSoftware.com

by naspinski 5/25/2011 2:20:00 PM

As I have been getting more and more freelance offers and work, the decision was made to launch a new website and brand

Yesterday I launched CohesiveSoftware.com as a contact point for custom work. Me and some fellow developers have teamed up on a few projects and decided to make it official. If you need any work done, please feel free to drop us a line - happy coding!

Random .Net SQL Generator (Open Source)

by naspinski 2/5/2011 6:42:00 PM

A random generator for all types of queries in SQL - extend-able to any SQL provider

I have often ran into the case where I need to insert a bunch of random records against a table or do a lot of queries to test for integrity - but those tools always seem to cost money. Some classmates and I did a project last semester that allowed for random SQL generation of all types. This is a generation tool for random queries in SQL in the Windows environment. It can produce random queries, tables, deletes, and updates as well as generate random data for a table. This was implemented with PostgresSQL, but we tried to structure it to allow easy adaptation for other SQL implementations as well.

The interface was meant to be very simple and requires no information about the tables themselves, just feed in basic information like the connectionstring and the table name to run the query against:

insert 100 random records

PgSqlGenerator pg = new PgSqlGenerator("127.0.0.1", 5432, 
    "postgres", "sql", "my_database");
Table people = pg.GetTable("people");
QueryInformation report = 
    pg.ExecuteBulkInsert(people, 100);

//now check the results:
Console.WriteLine("Query: " + report.Query);
Console.WriteLine("Time Taken: " + report.Time);
Console.WriteLine("Rows Affected: " + report.Affected);

It is far from bug-free, but works very well - figured we would share what we did.



ServerInfo - Easily scan a Machine/Server Farm for Information

by naspinski 7/31/2010 12:23:00 PM

new open-source project in Asp.Net MVC 2

More than once I have been asked what databases are on what server, if server X is running application Y or what websites server ABC is running. These are are relatively simple things to accomplish, and there are tools available to get this information, but this is an extremely simple and portable solution - all the data is kept in xml, so there is no need to install a backend.

All that it is required to get all this information is to enter ip addresses and the user has the rights to scan the machines requested.

In addition, this can keep track of all of the owners of the machines and has a GUI for running WMI Queries, which is extremely powerful if you know how to use it.

This is written with Asp.Net MVC 2, C# and xml; it requires .Net 4.0 framework. I will be updating this to MVC 3/Razor in the near future.

Tags: , , ,

c# | mvc | my projects | xml

Edit an Object Property Value Dynamically at Run Time

by naspinski 6/1/2010 12:13:00 PM

no need to write a huge switch statement to make a run time decision

Imagine you have an Object with 50 properties, and at runtime, you only need to change the value of one of them. You could write a switch statement to run through all of them, but there is a better way. This is how it would be done with a switch:
switch(propertyName)
{
    case "Name": obj.Name = newVal; break;
    case "Phone": obj.Phone = newVal; break;
    ///and so on...

That sounds like a terrible idea. With Reflection, it was easy to build an extension to update any object with a new value at runtime (provided it can be written to) with the following code:
public static void SetPropertyValue(this object o, 
    string propertyName, object newValue)
{
    PropertyInfo pi;
    pi = o.GetType().GetProperty(propertyName);
    if (pi == null)
        throw new Exception("No Property [" + 
            propertyName + "] in Object [" + 
            o.GetType().ToString() + "]");
    if (!pi.CanWrite)
        throw new Exception("Property [" + 
            propertyName + "] in Object [" + 
            o.GetType().ToString() + 
            "] does not allow writes");
    pi.SetValue(o, newValue, null);
}

Now, instead of that 50+ line mess above, all you need to do to change the "Phone" property to 'newVal' is:
obj.SetPropertyValue("Phone", newVal);

I added this to my Naspinski.Utilities Set on CodePlex as well.