Search code examples
c#asp.netavatargravatar

Gravatar Image not displaying correctly on ASP.net website


I have a website developed by using asp.net , c#

In there I made my own comment system. If user want to post a comment He has to enter comment, Name and email.

enter image description here

When I load the comments I am using this code to load the gravator.

using System.Security.Cryptography;

/// Hashes an email with MD5.  Suitable for use with Gravatar profile
/// image urls
public static string HashEmailForGravatar(string email)
{
    // Create a new instance of the MD5CryptoServiceProvider object.  
    MD5 md5Hasher = MD5.Create();

    // Convert the input string to a byte array and compute the hash.  
    byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(email));

    // Create a new Stringbuilder to collect the bytes  
    // and create a string.  
    StringBuilder sBuilder = new StringBuilder();

    // Loop through each byte of the hashed data  
    // and format each one as a hexadecimal string.  
    for(int i = 0; i < data.Length; i++)
    {
        sBuilder.Append(data[i].ToString("x2"));
    }

    return sBuilder.ToString();  // Return the hexadecimal string. 
}

Then using this code to assign it

//  Compute the hash
string hash = HashEmailForGravatar(email);

//  Assemble the url and return
return string.Format("http://www.gravatar.com/avatar/{0}", hash);

But all I am getting is this default blue image. enter image description here

Here I am explaining the assining part This is the element on ASPX page

 <asp:Image runat="server" ImageUrl='<%#Eval("GravatorURL")%>' />

This is where I am assiging value to that Eval part

DataTable dt = new DataTable();
                dt = objBlog_BLL.GetArticleComment(articleID);
                dt.Columns.Add("GravatorURL", typeof(String));
                foreach (DataRow dr in dt.Rows)
                {
                    string CommenterEmail = dr["author_email"].ToString();
                    string hash = HashEmailForGravatar(CommenterEmail);

                    string myGravatar = string.Format("//www.gravatar.com/avatar/{0}?size=50", hash);
                    dr["GravatorURL"] = myGravatar;
                }
                dListComment.DataSource = dt;
                dListComment.DataBind();

THis is the HTML tag rendering on client side

<img src="//www.gravatar.com/avatar/4F3FFEF1297E4BF2F746007DFCD36FA5?size=50">

I lowered this result by using .LoverCase() . but still the result is same.

But this email address has an image. I found an another site called Avatarapi.com. In there you can enter the email address and check the image. It is fully working. THey also provide a API to do this. All you have to do is using this code

<script src="https://www.avatarapi.com/[email protected]&size=128">
</script>

But problem is they are pointing the URL back to their site when you hover on the gravator. So I need a clean method. Whats wrong with my code?


Solution

  • Your code works just fine for producing my gravatar if it's applied to an all-lowercase email address. There are just a few things I'd check:

    1. Gravatar's docs show the initial email address needing to be trimmed and converted to lowercase prior to hashing. Try passing email.Trim().ToLowerInvariant() to the GetBytes method in the first place.
    2. You haven't shown the actual string produced by your string.Format(). It's possible (though unlikely) that your culture settings could be affecting how the string is represented. You could pass a CultureInfo.Invariant argument to string.Format(), or just switch to concatenating the strings directly with the + operator.
    3. You haven't shown the actual HTML tag that gets rendered in your browser. There's a possibility that something client-side is not reading or applying the server's response correctly.

    Update

    According to https://www.avatarapi.com/:

    This API uses public profile information provided by Google, via a publicly exposed Google data source to determine the name and profile image of the user. There are over 1 billion profile pics available via this API. The API works best with gmail addresses, but many other email accounts are registered with Google.

    So you're asking about producing a Gravatar image, but you think you're not getting a valid result because you're comparing your results against a service that uses Google Profile images, and only falls back on gravatar as a backup option.