记录用户 使用的是cookie,先写个cookie操作的类
WCF RIA 登录验证+cookie之完整登录验证加保留用户状态
在之前我曾经发个两个帖子说明登录验证问题,这里我再讲讲如何记录下登录的用户。如果不记录下用户会有很多问题,例如刷新。 一个基于wcf ria的简单登录方式 http://bbs.silverlightchina.net/ ... 1fromuid=45530 以及 WCF RIA 登录验证升级版 http://bbs.silverlightchina.net/ ... 2fromuid=45530 记录用户我使用的是co
在之前我曾经发个两个帖子说明登录验证问题,这里我再讲讲如何记录下登录的用户。如果不记录下用户会有很多问题,例如刷新。
一个基于wcf ria的简单登录方式
http://bbs.silverlightchina.net/ ... 1&fromuid=45530
以及
WCF RIA 登录验证升级版
http://bbs.silverlightchina.net/ ... 2&fromuid=45530
记录用户我使用的是cookie,先写个cookie操作的类。
cookie.cs
在mainpage.cs中加入
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);
}