Ryan LanciauxNew Media Mercenary

Gravatar on ASP.NET MVC

November 9, 2008 by ryan

UPDATE: See here

In the near future, I have a web project coming out that I've been working on for the past month or so. I'm not going to give too much detail on that just yet, however, in working on this project, I had to come up with a way for users to have profile images. I would rather not host these images on my server, I decided to leverage Gravatar (Globally Recognized Avatars).

There are a couple pre-existing options for dealing with gravatar in the .NET framework, however, I'm using the MVC framework -- standard ASP.NET controls are out of the question. After a brief look on the Gravatar site (specifically the page dealing with how the URL is constructed), it seemed like this would be a pretty easy task to write a class that I could use to get gravatar image paths. Below is the main part of the class I created to retrieve gravatar information based on an e-mail address:


        public static string GetGravatarURL(string email, string size)

        {

            return (string.Format("http://www.gravatar.com/avatar/{0}?s={1}&r=PG",

                EncryptMD5(email), size));

        }

 

        public static string GetGravatarURL(string email, string size, string defaultImagePath)

        {

            return GetGravatarURL(email, size) +  string.Format("&default={0}", defaultImagePath);

        }

 

        private static string EncryptMD5(string Value)

        {

            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] valueArray = System.Text.Encoding.ASCII.GetBytes(Value);

            valueArray = md5.ComputeHash(valueArray);

            string encrypted = "";

            for (int i = 0; i < valueArray.Length; i++)

                encrypted += valueArray[i].ToString("x2").ToLower();

            return encrypted;

        }


All we're really doing is creating a URL based on the Gravatar path, an MD5 encrypted version of an e-mail address and some user specified parameters. For the methods, Size is how many pixels the image should be (up to 500) and defaultImagePath is the url to use as an alternate image if the user does not have a Gravatar image. There is a rating parameter also but for my site, I'm setting it to PG always.

This is not all that difficult but hopefully useful. I plan to continue writing examples as I make progress on the web application I mentioned.


kick it on DotNetKicks.com



Related posts

Comments

November 11. 2008 17:18

Simone

Sorry, maybe it's my problem, but I don't see how this relates to ASP.NET MVC: that's just a standalone method that creates the gravatar url

Simone

November 11. 2008 17:31

ryan

Hey Simone -- Yea, you are correct Smile it's not directly related to MVC, however, if I was not using MVC I could have simply used one of the pre-existing controls. I guess I wasn't too clear in the post but I wanted a way to pass the Gravatar info to my view.

ryan

November 12. 2008 08:57

pingback

Pingback from alvinashcraft.com

Dew Drop - November 12, 2008 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

November 12. 2008 10:33

Ben Scheirman

You could make this a view helper...

Html.Gravatar("some@email.com")

Ben Scheirman

November 12. 2008 10:35

Ryan Lanciaux

I like that idea. I think I may give that a shot tonight Smile

Ryan Lanciaux

November 12. 2008 10:45

Simone

Yep, a Html.Gravatar("some@email.com") that also renders the img tag and not only the url of the image.
And you can accept an anonymous type (as the other Html helpers do) so that devs can supply arbitrary html attributes (like class, style, onclick events and so on)

Simone

November 13. 2008 07:22

trendbender

very usefull code for my blog, thx Smile

trendbender

November 14. 2008 11:14

James Curran

Funny, just two weeks ago, I wrote one for Castle MonoRail.

using.castleproject.org/display/Contrib/Gravatar+Component

SVN Repo:
http://svn.castleproject.org:8080/svn/castlecontrib/viewcomponents/trunk/Castle.MonoRail.ViewComponents/GravatarViewComponent.cs/

(Don't worry; I'm not accusing you of anything -- the code bases are completely different!)

James Curran

Comments are closed




© 2008 Ryan Lanciaux :: powered by BlogEngine.NET