方法1: session
程式碼區塊
login.aspx.cs
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Session["UserID"] = Login1.UserName;
}
{
Session["UserID"] = Login1.UserName;
}
程式碼區塊
default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
{
if ((string)Session["UserID"] != null)
{
string LgnName = (string)Session["UserID"];
if (Roles.IsUserInRole(LgnName, "Boss"))
Literal1.Text = "hi,Boss";
else if (Roles.IsUserInRole(LgnName, "Manager"))
Literal1.Text = "hi,Manager";
else Literal1.Text = "hi~";
}
{
string LgnName = (string)Session["UserID"];
if (Roles.IsUserInRole(LgnName, "Boss"))
Literal1.Text = "hi,Boss";
else if (Roles.IsUserInRole(LgnName, "Manager"))
Literal1.Text = "hi,Manager";
else Literal1.Text = "hi~";
}
方法2:COOKIE
程式碼區塊
login.aspx.cs
protected void Login1_LoggedIn(object sender, EventArgs e)
{
//cookie
DateTime now = DateTime.Now;
HttpCookie MyCookie = new HttpCookie("UserName");
MyCookie.Value = Login1.UserName;
MyCookie.Expires = now.AddHours(1);//Cookie的到期
Response.Cookies.Add(MyCookie);
}
{
//cookie
DateTime now = DateTime.Now;
HttpCookie MyCookie = new HttpCookie("UserName");
MyCookie.Value = Login1.UserName;
MyCookie.Expires = now.AddHours(1);//Cookie的到期
Response.Cookies.Add(MyCookie);
}
程式碼區塊
default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//cookie
if (Request.Cookies.Get("UserID") != null)
{
HttpCookie MyCookie = Request.Cookies.Get("UserID");
Literal1.Text = "MyCookie=" + MyCookie.Value;
}
{
//cookie
if (Request.Cookies.Get("UserID") != null)
{
HttpCookie MyCookie = Request.Cookies.Get("UserID");
Literal1.Text = "MyCookie=" + MyCookie.Value;
}
}
沒有留言:
張貼留言