在以前我曾經發個兩個帖子說明登陸驗證問題,這裏我再講講如何記錄下登陸的用戶。若是不記錄下用戶會有不少問題,例如刷新。web
一個基於wcf ria的簡單登陸方式瀏覽器
http://bbs.silverlightchina.net/ ... 1&fromuid=45530cookie
以及ui
WCF RIA 登陸驗證升級版spa
http://bbs.silverlightchina.net/ ... 2&fromuid=45530.net
記錄用戶我使用的是cookie,先寫個cookie操做的類。orm
cookie.cs進程
在mainpage.cs中加入ci
public MainPage()
{
InitializeComponent();
//驗證是否已經登陸?讀cookie
if(cookie.excits( "username",_username) //進入用戶儀表盤
else //留在登陸頁面
}
//登陸按鈕點擊後
private void button1_Click( object sender, RoutedEventArgs e)
{
myds.checklogin(login1.textBox1.Text,login1.passwordBox1.Password, (xx) =>
{
if (xx.Value)
{
//登陸成功後的處理,顯示其餘控件,隱藏登陸控件
login1.Visibility = Visibility.Collapsed;
logout.Visibility = Visibility.Visible; //顯示退出控件
//寫cookie
cookie.Write( "username", login1.textBox1.Text, -1); //永久保留cookie
}
else login1.textBlock3.Text = "用戶名或者密碼錯誤";
}, null);
}
//退出按鈕點擊,返回登陸頁面,清空cookie
private void button2_Click( object sender, RoutedEventArgs e)
{
//清除cookie
logout.Visibility = Visibility.Collapsed;
login1.Visibility = Visibility.Visible;
login1.textBox1.Text = "";
login1.passwordBox1.Password = "";
cookie.Delete( "username");
}
public class cookie
{
//是否存在
public static bool Exists( string key, string value)
{
return HtmlPage.Document.Cookies.Contains(key + "=" + value);
}
//讀
public static string Read( string key)
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
foreach ( string cookie in cookies)
{
string[] keyValuePair = cookie.Split('=');
if (keyValuePair.Length == 2 && key == keyValuePair[0].Trim())
return keyValuePair[1].Trim();
} return null;
}
//寫
public static void Write( string key, string value, int expireDays)
{
// expireDays = 0, 瀏覽器進程
// expireDays = -1, 永久保留
// expireDays = n, 保留n天
string expires = "";
if (expireDays != 0)
{
DateTime expireDate = (expireDays > 0 ?
DateTime.Now + TimeSpan.FromDays(expireDays) :
DateTime.MaxValue);
expires = ";expires=" + expireDate.ToString( "R");
} string cookie = key + "=" + value + expires;
HtmlPage.Document.SetProperty( "cookie", cookie);
}
//刪
public static void Delete( string key)
{
DateTime expireDate = DateTime.Now - TimeSpan.FromDays(1);
// yesterday
string expires = ";expires=" + expireDate.ToString( "R");
string cookie = key + "=" + expires;
HtmlPage.Document.SetProperty( "cookie", cookie); }
{
InitializeComponent();
//驗證是否已經登陸?讀cookie
if(cookie.excits( "username",_username) //進入用戶儀表盤
else //留在登陸頁面
}
//登陸按鈕點擊後
private void button1_Click( object sender, RoutedEventArgs e)
{
myds.checklogin(login1.textBox1.Text,login1.passwordBox1.Password, (xx) =>
{
if (xx.Value)
{
//登陸成功後的處理,顯示其餘控件,隱藏登陸控件
login1.Visibility = Visibility.Collapsed;
logout.Visibility = Visibility.Visible; //顯示退出控件
//寫cookie
cookie.Write( "username", login1.textBox1.Text, -1); //永久保留cookie
}
else login1.textBlock3.Text = "用戶名或者密碼錯誤";
}, null);
}
//退出按鈕點擊,返回登陸頁面,清空cookie
private void button2_Click( object sender, RoutedEventArgs e)
{
//清除cookie
logout.Visibility = Visibility.Collapsed;
login1.Visibility = Visibility.Visible;
login1.textBox1.Text = "";
login1.passwordBox1.Password = "";
cookie.Delete( "username");
}
public class cookie
{
//是否存在
public static bool Exists( string key, string value)
{
return HtmlPage.Document.Cookies.Contains(key + "=" + value);
}
//讀
public static string Read( string key)
{
string[] cookies = HtmlPage.Document.Cookies.Split(';');
foreach ( string cookie in cookies)
{
string[] keyValuePair = cookie.Split('=');
if (keyValuePair.Length == 2 && key == keyValuePair[0].Trim())
return keyValuePair[1].Trim();
} return null;
}
//寫
public static void Write( string key, string value, int expireDays)
{
// expireDays = 0, 瀏覽器進程
// expireDays = -1, 永久保留
// expireDays = n, 保留n天
string expires = "";
if (expireDays != 0)
{
DateTime expireDate = (expireDays > 0 ?
DateTime.Now + TimeSpan.FromDays(expireDays) :
DateTime.MaxValue);
expires = ";expires=" + expireDate.ToString( "R");
} string cookie = key + "=" + value + expires;
HtmlPage.Document.SetProperty( "cookie", cookie);
}
//刪
public static void Delete( string key)
{
DateTime expireDate = DateTime.Now - TimeSpan.FromDays(1);
// yesterday
string expires = ";expires=" + expireDate.ToString( "R");
string cookie = key + "=" + expires;
HtmlPage.Document.SetProperty( "cookie", cookie); }