Skip to content

Instantly share code, notes, and snippets.

@conficient
Last active June 10, 2022 11:47
Show Gist options
  • Save conficient/93f6f85a2bd0af645ccee0f99cee9424 to your computer and use it in GitHub Desktop.
Save conficient/93f6f85a2bd0af645ccee0f99cee9424 to your computer and use it in GitHub Desktop.
A Razor component that displays a Gravatar image for a user's email address
@* Creates a Gravatar image using Bootstrap styling from email address *@
<img class="rounded-circle" src="@url" />
@code
{
[Parameter] public string Email { get; set; }
[Parameter] public int Size { get; set; } = 24;
protected override void OnParametersSet()
{
// create hash
var bytes = System.Text.Encoding.UTF8.GetBytes(Email.ToLower());
var hash = System.Security.Cryptography.MD5.HashData(bytes);
var hex = string.Join("", hash.Select(x => x.ToString("x2")));
url = $"https://www.gravatar.com/avatar/{hex}?s={sizeClean}";
}
const int minSize = 8;
private int sizeClean => Size>= minSize ? Size: minSize;
private string url = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment