/// <summary> /// Converts an OpenID to a libravatar URI /// </summary> /// <param name="openid"> /// The OpenID associated with the avatar you want to retrieve /// </param> /// <param name="options"> /// An AvatarOptions object, which provides various ways to customize the behavior /// </param> /// <returns> /// A URI which points to the user's avatar. /// </returns> public static Uri FromOpenID(string openid, AvatarOptions options) { var identity = CanonicalizeOpenID(openid); var hashFunction = (Func <string, string>)SHA256Hash; // MD5 is not supported for OpenID. return(FromHashedIdentity(hashFunction(identity), options)); }
public static void Main(string[] args) { // 1. simple case: email in, gravatar-style output var uri = AvatarUri.FromEmail( "*****@*****.**" ); Console.WriteLine (uri); // 2. use a different hash var uri2 = AvatarUri.FromEmail( "*****@*****.**", new AvatarOptions { UseSHA256 = true } ); Console.WriteLine( uri2 ); // 3. you can also use HTTPS var uri3 = AvatarUri.FromEmail( "*****@*****.**", new AvatarOptions { PreferHttps = true } ); Console.WriteLine( uri3 ); // 4. AvatarOptions is just a normal C# object, // so you can create one for the options you // want, and store it / give it a name: var favoriteOptions = new AvatarOptions { UseSHA256 = true, PreferHttps = true }; var uri4 = AvatarUri.FromEmail( "*****@*****.**", favoriteOptions ); Console.WriteLine( uri4 ); // 5. You can do monsters, wavatars, identicons, etc: var uri5 = AvatarUri.FromEmail( "*****@*****.**", new AvatarOptions { DefaultImage = AvatarDefaultImages.MonsterID } ); Console.WriteLine( uri5 ); // 6. If you need a size other than the default, you can specify that: var uri6 = AvatarUri.FromEmail( "*****@*****.**", new AvatarOptions { Size = 32 } ); Console.WriteLine( uri6 ); }
/// <summary> /// Converts an email address to a libravatar URI /// </summary> /// <param name="email"> /// The email address associated with the avatar you want to retrieve /// </param> /// <param name="options"> /// An AvatarOptions object, which provides various ways to customize the behavior /// </param> /// <returns> /// A URI which points to the user's avatar. /// </returns> public static Uri FromEmail(string email, AvatarOptions options) { var identity = CanonicalizeEmail(email); var hashFunction = options.UseSHA256 ? (Func <string, string>)SHA256Hash : MD5Hash; return(FromHashedIdentity(hashFunction(identity), options)); }
static Uri FromHashedIdentity(string hash, AvatarOptions options) { var args = new Dictionary <string, string>(); if (options.DefaultImage != null) { args["d"] = options.DefaultImage; } if (options.Size != null) { args["s"] = options.Size.ToString(); } var baseUri = options.PreferHttps ? options.SecureBaseUri : options.BaseUri; var uri = baseUri + hash + UriQueryFromArgs(args); return(new Uri(uri)); }
static Uri FromHashedIdentity( string hash, AvatarOptions options ) { var args = new Dictionary<string,string>(); if (options.DefaultImage != null) args["d"] = options.DefaultImage; if (options.Size != null) args["s"] = options.Size.ToString(); var baseUri = options.PreferHttps ? options.SecureBaseUri : options.BaseUri; var uri = baseUri + hash + UriQueryFromArgs(args); return new Uri(uri); }
/// <summary> /// Converts an OpenID to a libravatar URI /// </summary> /// <param name="openid"> /// The OpenID associated with the avatar you want to retrieve /// </param> /// <param name="options"> /// An AvatarOptions object, which provides various ways to customize the behavior /// </param> /// <returns> /// A URI which points to the user's avatar. /// </returns> public static Uri FromOpenID( string openid, AvatarOptions options ) { var identity = CanonicalizeOpenID(openid); var hashFunction = (Func<string,string>)SHA256Hash; // MD5 is not supported for OpenID. return FromHashedIdentity( hashFunction(identity), options ); }
/// <summary> /// Converts an email address to a libravatar URI /// </summary> /// <param name="email"> /// The email address associated with the avatar you want to retrieve /// </param> /// <param name="options"> /// An AvatarOptions object, which provides various ways to customize the behavior /// </param> /// <returns> /// A URI which points to the user's avatar. /// </returns> public static Uri FromEmail( string email, AvatarOptions options ) { var identity = CanonicalizeEmail(email); var hashFunction = options.UseSHA256 ? (Func<string,string>)SHA256Hash : MD5Hash; return FromHashedIdentity( hashFunction(identity), options ); }