Implement Gravatar icons in ASP.NET

Mar 25, 2007

Gravatar icons can be used on all websites that lets users write comments. It could be in a forum or on a blog. The image is retrieved from the Gravatar website and is based on the e-mail address of the person that makes the comment.

It is very easy to set up because it is just a normal image where the src attribute point to the Gravatar website. Here is a method that writes out such an image. It takes an e-mail address and a size. The default size used by Gravatar is 80 (80x80 pixels), but you can specify a smaller size if you’d like.

protected string Gravatar(string email, int size)
{
  System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
  byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(email));

  System.Text.StringBuilder hash = new System.Text.StringBuilder();
  for (int i = 0; i < result.Length; i++)
    hash.Append(result[i].ToString("x2"));

  System.Text.StringBuilder image = new System.Text.StringBuilder();
  image.Append("<img src=\"");
  image.Append("http://www.gravatar.com/avatar.php?");
  image.Append("gravatar_id=" + hash.ToString());
  image.Append("&amp;rating=G");
  image.Append("&amp;size=" + size);
  image.Append("&amp;default=");
  image.Append(Server.UrlEncode("http://example.com/noavatar.gif"));
  image.Append("\" alt=\"\" />");
  return image.ToString();
}

On the webpage you could then write out the Gravatar image like so:

<%=Gravatar("post@example.com", 60)%>

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (7) -

Sean Kearney
Sean Kearney
3/26/2007 11:51:12 AM #

Hi Mads,
Very quick and easy implementation, nice!

I created a control for Gravatars not all the long ago. Check it out here:
www.carknee.com/.../asp-net-gravatar-control.aspx

-Sean

Mads Kristensen
Mads Kristensen
3/26/2007 12:40:14 PM #

Sean, I looked at your control before creating this snippet. I just needed a single method instead of a control.

 Mike
Mike
3/28/2007 5:28:27 PM #

One small suggestion:

string hash = BitConverter.ToString(result);

 Thomas Hansen
Thomas Hansen
3/29/2007 11:53:24 AM #

We've got a "DynamicImage" control which is pretty nice for doing that... Smile
Check it out at:
ajaxwidgets.com/.../DynamicImage.aspx

.t

Fredrik Kalseth
Fredrik Kalseth
4/5/2007 7:06:58 AM #

This looks pretty cool Smile

But what about security issues? Could I not just type in the email address of someone else, and pretend to be them?

Sean Kearney
Sean Kearney United States
5/11/2007 3:23:17 PM #

Fredrik,
What I did was create a "random" email that I use for Gravatars so it would be almost impossible for someone to guess.

Iv&#225;n
Iván Spain
1/6/2009 2:36:07 PM #

Thank you!, it took me 1 minute to make it work! (you saved me some time on this Smile

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.