日期:2014-05-18 浏览次数:20826 次
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string us = Users.Text; string ps = Password.Text; if (checkusers(us, ps)) { //FormsAuthentication.RedirectFromLoginPage(us,false); FormsAuthenticationTicket myTicket = new FormsAuthenticationTicket( 1, us, System.DateTime.Now, System.DateTime.Now.AddDays(30), false, "admin", "/"); string myEncrypt = FormsAuthentication.Encrypt(myTicket); HttpCookie myHttpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, myEncrypt); this.Response.Cookies.Add(myHttpCookie); if (HttpContext.Current.Request["ReturnUrl"] == null) { this.Response.Redirect("default.aspx"); } else { this.Response.Redirect(HttpContext.Current.Request["ReturnUrl"].ToString()); } } } bool checkusers(string users, string password) { return true; } } using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string name = getTicket().Name; this.Response.Write(name); } FormsAuthenticationTicket getTicket() { string FormsCookieName = FormsAuthentication.FormsCookieName; HttpCookie myHttpCookie = HttpContext.Current.Request.Cookies[FormsCookieName]; if (myHttpCookie == null) { return null; } else { return FormsAuthentication.Decrypt(myHttpCookie.Value); } } }