每个项目在制作中常常都有登录,注册,管理员(模态对话框下)功能:
首先是登录:
1.点击登录,弹出新的登录页面或则模态登录框,输入账号密码,点击登录,若成功则跳到新的页面,若失败则提示错误。
主页下有:
<div id="global">
<div id="ui_top" class="btn-group">
<button id="state" type="button" class="btn btn-info">@ViewData["name"].ToString()</button> /代表登录,
<button id="state2" type="button" class="btn btn-info">@ViewData["regi"].ToString()</button> ///代表注册,这里使用从页面传来的数据,主要是为了动态生成字
<button id="state3" type="button" class="btn btn-info" data-toggle="modal" data-target=".bs-example-modal-sm">管理</button>
</div>
<script type="text/javascript">
$("#state").bind("click", function () {
var tip = document.getElementById("state").innerHTML;
if (tip == "登录")
window.location.href = "/Account/Index";
else
window.location.href = "/User/Index";
});
$("#state2").bind("click", function () {
var tip = document.getElementById("state2").innerHTML;
if (tip == "注册")
window.location.href = "/Account/Regi";
else
window.location.href = "/Home/Exit";
});
</script>
</div>
登录页面:
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="zh-cn" >
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<style type="text/css" >
*{font-family:"微软雅黑";}
body {background-image:url(/Resources/img/background.jpg);background-repeat:no-repeat;background-size:cover ;}
#login {
position :absolute ;
top:50%;
left:48%;
width :360px;
margin-top:-120px;
margin-left:-160px;
}
#result {
color :red;
}
</style>
<script>
function afterSuccess() {
if (document.getElementById("result").innerText == "登录成功!")
window.location.href = "/User/Index";
}
</script>
<link rel="shortcut icon" href="/Resources/img/logo_0.jpg"/>
<title>登陆</title>
</head>
<body>
<div>
<div id="login">
<div class="panel panel-default" >
<div class="panel-heading" ><b>用户登陆</b></div>
<div class="panel-body">
@using (Ajax.BeginForm("Login", "Account", new AjaxOptions(){HttpMethod = "Post",InsertionMode = InsertionMode.Replace ,UpdateTargetId = "result",OnSuccess="afterSuccess"})) //UpdateTargetId用于获取控制传回来的Content, OnSuccess(),传递成功执行的函数
{
<div class="form-group">
<label >账号</label>
<input name="name" type="text" class="form-control" />
</div>
<div class="form-group">
<label >密码</label>
<input name="password" type="password" class="form-control" />
</div>
<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="submit" class="btn btn-primary">登录</button><label > <span id="result"></span></label>
}
</div>
</div>
</div>
</div>
</body>
</html>
登录对应的控制器动作有:
public ActionResult Login()
{
string name = Request["name"];
string password = Request["password"];
DataTable table = SqlHelper.ExecuteDataTable("select * from T_UserInfo wherename=@name",
new SqlParameter("@name", name));
if (table.Rows.Count <= 0)
{
return Content("用户不存在!");
}
DataRow row = table.Rows[0]; //定位到输入用户的行
string pwdDB = (string)row["pwd"];//将改行的pwd赋值给pwdDB
if (pwdDB != password)//若密码不与输入的密码相等
{
return Content("用户名或密码错误!"); //密码错误
}
else
{
Response.Cookies["GTBS_name"].Value = name;//输入的值存到Cookies中
Response.Cookies["GTBS_name"].Expires = DateTime.Now.AddDays(1);
return Content("登录成功!");//返回内容传给原来的页面
}
}
注册页面:
@{
Layout = null;
}
<!--注册的页面-->
<!DOCTYPE html>
<html lang="zh-cn" >
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
<script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<style type="text/css" >
*{font-family:"微软雅黑";}
body {background-image:url(/Resources/img/background_re.jpg);background-repeat:no-repeat ;background-size:cover ;}
#login {
position :absolute ;
top:50%;
left:48%;
width :360px;
margin-top:-120px;
margin-left:-160px;
}
#result {
color :red;
}
</style>
<script>
function afterSuccess() {
if (document.getElementById("result").innerText == "注册成功!")
window.location.href = "/User/Index";
}
</script>
<link rel="shortcut icon" href="/Resources/img/logo_0.jpg"/>
<title>注册</title>
</head>
<body>
<div>
<div id="login">
<div class="panel panel-default" >
<div class="panel-heading" ><b>用户注册</b></div>
<div class="panel-body">
@using (Ajax.BeginForm("Regi_Result", "Account", new AjaxOptions(){HttpMethod = "Post",InsertionMode = InsertionMode.Replace ,UpdateTargetId = "result",OnSuccess="afterSuccess"}))
{
<div class="form-group">
<label >设置账号</label>
<input name="name_re" type="text" class="form-control" />
</div>
<div class="form-group">
<label >设置密码</label>
<input name="password_re" type="password" class="form-control" />
</div>
<!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
<button type="submit" class="btn btn-primary">注册</button><label > <span id="result"></span></label>
}
</div>
</div>
</div>
</div>
</body>
</html>
注册页面对应的控制器下动作:
public ActionResult Regi_Result()
{
string name = Request["name_re"];
string pwd = Request["password_re"];
DataTable table = SqlHelper.ExecuteDataTable("select * from T_UserInfo wherename=@name",
new SqlParameter("@name", name));
if (table.Rows.Count >= 1)
{
return Content("此用户名已存在!");
}
else
{
System.Guid guid = new Guid();
guid = Guid.NewGuid();
string str = guid.ToString(); //生成guid
SqlHelper.ExecuteNonQuery("insert into T_UserInfo(id,name,pwd) values(@id,@name,@pwd)", new SqlParameter("@id", str), new SqlParameter("@name", name), new SqlParameter("@pwd", pwd));
Response.Cookies["GTBS_name"].Value = name;
Response.Cookies["GTBS_name"].Expires = DateTime.Now.AddDays(1);
return Content("注册成功!");
}
}