Asp.Net(5):使用全球通Gravatar头像
08 Jun 2012在WP中留言大多数网友都会有一个Gravatar头像,方便使用头像。
今天忽然想起来,用Asp.Net折腾一下,来吧。
扫盲:关于Gravatar
先下载控件动态链接库: 下载
新建web应用程序,引入下载的库文件。
在页首加入如下代码:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”Default.aspx.cs” Inherits=”Gavatar.Default” %>
<%@ Register Assembly=”FreshClickmedia.Web” Namespace=”FreshClickMedia.Web.UI.WebControls” TagPrefix=”fcm” %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Gavatar.Default" %> <%@ Register Assembly="FreshClickmedia.Web" Namespace="FreshClickMedia.Web.UI.WebControls" TagPrefix="fcm" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Image ID="Image" runat="server" /> <asp:TextBox ID="Email" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="提交" onclick="Button1_Click" /> </div> </form> </body> </html>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using FreshClickMedia.Web.UI.WebControls; using System.Web.Security; namespace Gavatar { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } protected void Button1_Click(object sender, EventArgs e) { string email = this.Email.Text; string authen = FormsAuthentication.HashPasswordForStoringInConfigFile(email,"MD5"); string imageurl = "http://www.gravatar.com/avatar.php?gravatar_id=" + authen + "&rating=G&size=80&default=http://www.silverlight.net/content/general/events/scottgu.jpg"; Image.ImageUrl = imageurl.ToLower();//注意,加密后后默认为大写,,需要转成小写 } } }
效果图:
gravatar_id:Email地址的MD5
rating:允许头像的级别
size:头像的大小
default:默认头像的URL
转载请注明:于哲的博客 » Asp.Net(5):使用全球通Gravatar头像