登陆与注册以及Session

时间:2023-03-09 16:39:28
登陆与注册以及Session

Session  保存状态是在 登陆窗口  检查用户密码的动作上执行

、Models
namespace 注册与登陆以及Session.Models
{
public class UserBF
{
private MyDBDataContext _context = new MyDBDataContext();
public bool Select(string username,string password) //验证用户名和密码是否正确(登陆)
{
var query = _context.logins.Where(P=>P.UserName==username&&P.PassWord==password);
return query.Count() > ;
} public void Insert(string UserName,string PassWord ) //注册
{
logins lo = new logins();
lo.UserName = UserName;
lo.PassWord = PassWord;
_context.logins.InsertOnSubmit(lo);
_context.SubmitChanges();
}
}
} 、Controllers
namespace 注册与登陆以及Session.Controllers
{
public class HomeController : Controller
{ public ActionResult Index()
{
return View();
}
public ActionResult Check(string username,string password) //登陆
{
bool isok= new UserBF().Select(username,password);
if(isok)
{ //保存状态
Session["user"] = username; //记录一下会话(登录状态) //跳转页面
return RedirectToAction("Html","Home");
}
else
{
return RedirectToAction("Index");
} }
public ActionResult Html() //登陆后的主页面
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "Home");
}
return View();
}
public ActionResult Login() //注册编辑动作
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "Home");
}
return View();
}
public ActionResult Insert(string username,string password)//注册动作
{
if (Session["user"] == null)
{
return RedirectToAction("Index", "Home");
}
new UserBF().Insert(username,password);
return RedirectToAction("Index");
}
}
} 、Views
Index:登陆界面
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style>
.aa
{
position:relative;
margin:auto;
width:400px;
height:400px; }
</style>
</head>
<body> @using(Html.BeginForm("Check","Home"))
{
<div class="aa">
用户名: @Html.TextBox("username","")<br>
密 码: &nbsp;@Html.Password("password","")<br>
<input id="Submit1" type="submit" value="登陆" />
<a href="/Home/Login"><input id="Button1" type="button" value="注册" /></a>
</div>
} </body>
</html> Login:注册页面
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Login</title>
<style>
.aa
{
position:relative;
margin:auto;
width:400px;
height:400px; }
</style>
</head>
<body>
@using(Html.BeginForm("Insert","Home"))
{
<div class="aa">
用户名 : &nbsp;@Html.TextBox("username","")<br>
密 码 : &nbsp;&nbsp;@Html.Password("password","")<br>
确认密码: @Html.Password("passwordagain","")<br>
<input id="Submit1" type="submit" value="注册" />
</div>
}
</body>
</html> Html:登陆后的主页面
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Html</title>
</head>
<body>
<div>
这是登陆后的页面 欢迎您: @Session["user"]
</div>
</body>
</html>

效果图:

登陆与注册以及Session

登陆与注册以及Session

登陆与注册以及Session

登陆与注册以及Session

登陆与注册以及Session