使用session实现登录判断功能
2017-04-15 20:17
363 查看
登录界面:
2个页面,一个index.aspx登录界面,一个view.aspx登录后的显示界面
如果一开始直接登录view.aspx,会显示没有登录,然后提示用户进入登录界面登录(index.aspx)
进入index.aspx界面注册:
如果用户名不为“winycg”,密码不为“123”,则显示用户名或密码不正确
输入用户名为“winycg”,密码为“123”,点击提交,跳转到view.aspx,显示登录成功
此时如果再次进入view.aspx界面后,还会显示登录成功,因为只要浏览器不关闭,session中存的值就不变
index.apsx代码://前台控件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
width: 53%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td>用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click"/>
</td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
index.apsx.cs代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.Trim();
string pwd = TextBox2.Text.Trim();
if(name=="")
{
Response.Write("<font color='red'>用户名不能为空</font>");
TextBox1.Focus();//获取焦点,光标会位于此处
return;
}
if(pwd=="")
{
Response.Write("<font color='red'>密码不能为空</font>");
TextBox2.Focus();
return ;
}
if(name=="winycg"&&pwd=="123")
{
Session["username"] = name;
Response.Redirect("view.aspx");
}
else
{
Response.Write("<font color='red'>用户名或密码不正确</font>");
}
}
view.aspx代码:
protected void Page_Load(object sender, EventArgs e)
{
if(Session["username"]==null)
{
Response.Write("<b><font color='red'>您不是合法用户!</font></b>");
Response.Write("<p><a href='index.aspx'>进入主页面</a>");
}
else
{
Response.Write("<b>恭喜登陆成功</b>");
}
}
2个页面,一个index.aspx登录界面,一个view.aspx登录后的显示界面
如果一开始直接登录view.aspx,会显示没有登录,然后提示用户进入登录界面登录(index.aspx)
进入index.aspx界面注册:
如果用户名不为“winycg”,密码不为“123”,则显示用户名或密码不正确
输入用户名为“winycg”,密码为“123”,点击提交,跳转到view.aspx,显示登录成功
此时如果再次进入view.aspx界面后,还会显示登录成功,因为只要浏览器不关闭,session中存的值就不变
index.apsx代码://前台控件
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
width: 53%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td>用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click"/>
</td>
<td> </td>
</tr>
</table>
</div>
</form>
</body>
</html>
index.apsx.cs代码:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string name = TextBox1.Text.Trim();
string pwd = TextBox2.Text.Trim();
if(name=="")
{
Response.Write("<font color='red'>用户名不能为空</font>");
TextBox1.Focus();//获取焦点,光标会位于此处
return;
}
if(pwd=="")
{
Response.Write("<font color='red'>密码不能为空</font>");
TextBox2.Focus();
return ;
}
if(name=="winycg"&&pwd=="123")
{
Session["username"] = name;
Response.Redirect("view.aspx");
}
else
{
Response.Write("<font color='red'>用户名或密码不正确</font>");
}
}
view.aspx代码:
protected void Page_Load(object sender, EventArgs e)
{
if(Session["username"]==null)
{
Response.Write("<b><font color='red'>您不是合法用户!</font></b>");
Response.Write("<p><a href='index.aspx'>进入主页面</a>");
}
else
{
Response.Write("<b>恭喜登陆成功</b>");
}
}
相关文章推荐
- 使用httpsessionlistener实现禁止账号多点登录功能
- 使用vue-router beforEach实现判断用户登录跳转路由筛选功能
- 一个使用监听器模式实现的J2ME网络编程框架,包括一个简单的登录功能实现(含源代码)
- tomcat 使用 memcached管理session ,并且实现统一登录
- 使用session技术来实现网上商城购物车的功能
- session、cookie与“记住我的登录状态”的功能的实现
- 使用PHP实现用户登录和注册的功能
- session、cookie与“记住我的登录状态”的功能的实现
- 使用Session记录页面地址和实现页面返回功能
- 一个使用监听器模式实现的J2ME网络编程框架,包括一个简单的登录功能实现(含源代码)
- 使用session判断用户登录用户权限(超简单)
- ASP.NET jQuery 食谱11 (通过使用jQuery validation插件简单实现用户登录页面验证功能)
- session 实现登录功能注意事项
- 使用(Filter)过虑器实现对Session是否过时的判断
- [JavaScript] session、cookie与“记住我的登录状态”的功能的实现
- session、cookie与“记住我的登录状态”的功能的实现
- 使用memcache实现单点登录(session共享)
- session、cookie与“记住我的登录状态”的功能的实现
- Rails实现一个blog项目02-登录功能和session的实现
- 使用SQL Server保存Session状态,实现单点登录