已经将代码放到这里.
https://skydrive.live.com/redir?resid=4D9CD22D50108C94!165
很简单的登录功能。环境visual studio 2012 ,4.5 framework,iis express
public static class Users
{
private static readonly List<Tuple<string, string, string>> vals;
static Users()
{
vals = new List<Tuple<string, string, string>>();
vals.Add(new Tuple<string, string, string>("userName", "pwd", "group1"));
}
public static Tuple<string, string, string> Login(string userName, string pwd)
{
return vals.FirstOrDefault(x => x.Item1 == userName && x.Item2 == pwd);
}
}
login.aspx登录页
protected void btnOk_Click(object sender, EventArgs e)
{
var result = Users.Login(userName.Text,pwd.Text);
if (result != null)
{
string userDataString = result.Item3;
HttpCookie authCookie = FormsAuthentication.GetAuthCookie(userName.Text, RememberMe.Checked);
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userDataString);
authCookie.Value = FormsAuthentication.Encrypt(newTicket);
Response.Cookies.Add(authCookie);
string redirUrl = FormsAuthentication.GetRedirectUrl(userName.Text, RememberMe.Checked);
Response.Redirect(redirUrl);
}
else
{
lblmsg.Text = "用户名或密码错误";