1

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/js.aspx?email=your_email@gmail.com&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?

Prageeth Liyanage
  • 1,612
  • 2
  • 19
  • 41
  • Have you looked at the `href` of your image to see if it's actually being inserted into the HTML the way you think it is? – StriplingWarrior Feb 22 '18 at 04:56
  • Yes I checked it – Prageeth Liyanage Feb 22 '18 at 05:03
  • what image control are you using? curious because it looks like the http is getting stripped off, but it isn't in my test. i'm using `` and `imgGravatar.ImageUrl = string.Format("http://www.gravatar.com/avatar/{0}", hash);`. Fwiw, i did strip the protocol (http) from the url, inside the browser, and it still worked so it might not matter at all, but it's odd. – wazz Feb 22 '18 at 05:45
  • @wazz I am using this – Prageeth Liyanage Feb 22 '18 at 06:03
  • Update the question with that aspx markup and show exactly how/when you're assigning the url. Explain the Eval part too. the plot thickens. – wazz Feb 22 '18 at 06:17
  • @wazz updated the question. Please check – Prageeth Liyanage Feb 22 '18 at 06:59
  • try adding http to see what happens. something strange is going on with your own email address, what about others? – wazz Feb 22 '18 at 07:43

1 Answers1

0

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.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Please check the question. Here is the HTML element – Prageeth Liyanage Feb 22 '18 at 05:14
  • +1 : I tried capitalizing a letter in the email address and i got the gravatar logo returned instead of a valid image. – wazz Feb 22 '18 at 05:48
  • @Sylar: Thanks for adding that information to the question. I'm going to assert that there is *no way* that the code you posted produced the avatar URL you ended up with. For one thing, your code uses a format string of "x2", which would produce a lower-case hash, whereas the URL you provided is all in upper-case. Besides that, your string.Format call includes "http://" at the front, but your url starts with "//". Unless you post [a minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) it's extremely difficult for anyone else to help you find your bug. – StriplingWarrior Feb 22 '18 at 06:29
  • @Sylar: Just as an example, your code produces http://www.gravatar.com/avatar/0cf15665a9146ba852bf042b0652780a when given the email address tomster@emberjs.com (pulled from [this question](https://stackoverflow.com/q/41786225/120955)). – StriplingWarrior Feb 22 '18 at 06:37
  • @StriplingWarrior updated the question. Please check – Prageeth Liyanage Feb 22 '18 at 06:59
  • that email does not work for me. maybe Avatarapi.com is caching old info, or supplying fake images when there is no image. is that image actually klprageeth? or one he supplied? – wazz Feb 22 '18 at 07:33
  • @Sylar: See my update. avatarapi pulls from Google Profile pics, so they'll have images for email addresses that don't have gravatar accounts. – StriplingWarrior Feb 22 '18 at 16:10