表单身份验证票证






2.33/5 (19投票s)
如何为不同用户管理不同的会话超时
引言
有时我们需要为每个用户设置不同的超时时间。当你给你的网页用户选择权,让他们自己定义超时时间时,每个用户都会设置自己的时间,你需要让你的网站根据用户指定的时间来维护每个用户的会话。
我们将做什么
在这里,我将解释如何使用 ASP.Net 的表单身份验证来实现这个目标。
问题
一些 ASP.NET 用户在使用身份验证票证时,会话超时时间显示出不一致的表现。 在这里,我将解释如何为不同的用户维护不同的超时时间。
什么是表单身份验证?
在 ASP 中,没有用于登录用户的机制,你只需要在会话中放入一些值
可以是用户 ID,然后在用户尝试访问的每个页面上检查此值。
但在 .net 中,表单身份验证提供了一种登录用户的机制。
登录时,你设置了一个加密的 cookie,你需要在该 cookie 中放入用户 ID,这
非常容易,之后 ASP.NET 会为你完成所有任务,无需在每个页面上检查
身份验证。
你需要做的是在 web.config 中指定一个登录页面,还有一些其他的设置
我将详细告诉你。 别担心,这些非常容易,可以在几
秒钟内完成。
让我们开始工作吧
创建 3 个页面
1. 创建 ASP.NET 项目(示例包含 C# 代码)
2. 创建登录页面 login.aspx。(如果请求未授权,表单身份验证会自动重定向到此页面)
3. 创建默认页面 default.aspx(每个站点都有一个默认页面,表单身份验证会在成功登录后自动重定向到此页面)。
4. 创建详情页面 details.aspx(可选页面,显示你的产品等)
一些页面将自动创建,例如
1. web.config
2. global.asax
你已经完成了网站的所有结构,干得好!!! 让我们现在开始编码吧!
开始编码
Web.config
首先,在 web.config 文件中做一些更改...
转到 web.config 的身份验证部分
i. 将身份验证代码设置为 forms
ii. 提供登录 URL,即你的登录页面,如果用户未登录并尝试访问某个页面,则将被重定向到该页面。
iii. 提供 passwordformat,现在将其设置为 clear,不要在这里混淆,这只是为了设置
你的密码将是清晰格式还是加密格式,这是另一个主题。
iv. 如果你有静态用户,请提供用户名,你也可以从数据库加载列表
稍后会告诉你如何操作。
<authentication mode="Forms" > <forms name=".ASPXSessionDemoTest" loginUrl="login.aspx" protection="All" > <credentials passwordFormat = "Clear"> <user name="admin" password="admin"/> </credentials> </forms> </authentication>Go to the section autorization of the web config. i.deny user set ? mark. so that it ask for password to each user.
<authorization> <deny users="?" /> <!-- Allow all users --> <!-- <allow users="[comma separated list of users]" roles="[comma separated list of roles]"/> <deny users="[comma separated list of users]" roles="[comma separated list of roles]"/> --> </authorization> We have alomost done with web.config file.
Login.aspx
当用户输入用户名
和密码并单击登录按钮时,编写以下代码(查找表单源代码)
// Create a custom FormsAuthenticationTicket containing
// application specific data for the user.
// user email address
string email = this.TextBox1.Text ;
//user password
string password = this.TextBox2.Text ;
bool isPersistent = false; //Persist.Checked;
//write down u r own methods for authentication
//either from database or file
if (Authenticat(email))
{
//if user is authunticated then
//get the user time from databse or file where user have
metiontioed it. this will return an intger value, for example 30, 60 this is time out in
minutes.
int timeout=settimeout();
//this variable will be accessible automatically
//on each form...
//so if you have some role base system
//or you want to assign some data to this user
//then put this along with this ticket
//as done here
//supose mubi is admin and i want store this info in ticket
//so that i can get it on each page
//you will get this information from database that what is
his roles. i am hard coding here.
string username="mubi";
string userData = setrole(username);
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket(
1,
email,
System.DateTime.Now,
System.DateTime.Now.AddMinutes(timeout),
isPersistent,
userData,
FormsAuthentication.FormsCookiePath);
// Encrypt the ticket.
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
Response.Cookies.Add(new
HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
// Redirect back to original URL.
Response.Redirect(FormsAuthentication.GetRedirectUrl(email,isPersistent));
注销按钮
按下注销按钮时,编写以下代码
//sign out from form authentication FormsAuthentication.SignOut(); //abandon session Session.Abandon(); Response.Redirect("logon.aspx");