Last active
June 10, 2022 11:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@* 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