【前言】
我们项目最近快要验收,验收的标准之一就是通过公司的安全检查!其中有一项标准:要求系统在登录五次失败后锁定系统!具体这个功能实现小编将在下篇博文为大家讲解。
组长把这个任务安排给我,于是我就开始了探索之旅,在探索过程中发现许多好玩的东西,后来又经过组长的点播有所收获,在此与大家共享。
【登录逻辑分析】
基础:
领完任务不敢稍有懈怠,打开代码看逻辑,首先不可绕过去的就是登录的逻辑:捋了半天,他丫的没找到在哪。。。非常郁闷~~~,好在经过组长点播:原来登录不是在这个项目中,而是在另外的项目,我们项目是通过引用DLL来通过那个项目控制。详情见下图:
登录逻辑详解:
1.界面表单提交:
<form class="form" id="signupForm">
<div class="search">
<form action="" method="post" class="form">
<div id="sea_text">
<input id="username" name="username" type="text" class="put" size="14" tabindex="1" placeholder="请输入用户名" value="@ViewBag.userName" style="background:#f5f6f7 url('../Content/Login/images/people.png') no-repeat 10px center; margin-left:10px">
<input type="password" id="userpass" name="userpass" class="put" size="14" tabindex="2" placeholder="不能小于{6}个字符!" value="" style="background:#f5f6f7 url('../Content/Login/images/key.png') no-repeat 10px center" />
<a href="javascript:void(0)" title="看不清,点击切换" style="line-height:30px;padding-bottom:5px;" onclick="changeCheckCode();return false;"><img id="imgCode" class="code" src="@Url.Action("GetValidateCode")?n=@System.DateTime.Now.Millisecond" /></a>
<input type="text" id="code" name="code" value="1111" placeholder="输入验证码" tabindex="3" class="put" size="14" style=" width:70px; text-indent:inherit; margin-left:0px;" />
</div>
<div id="btn"> <input name="button" type="button" class="btn1" tabindex="4" onclick="subm()" /></div>
<div class="clear"></div>
</form>
</div>
</form>
2.提交后执行的JS方法:
function subm() { <span style="font-family:KaiTi_GB2312;">//点击登录后进行操作</span>
var check = $("#signupForm").valid();
if (check) {
var strPassword = $.trim($("#userpass").val()); <span style="font-family:KaiTi_GB2312;"> //获取用户输入的密码</span>
var userName = $.trim($("#username").val()); //<span style="font-family:KaiTi_GB2312;">获取用户输入的用户名</span>
var code = $.trim($("#code").val()); //获取用户输入的<span style="font-family:KaiTi_GB2312;">验证码</span>
$.ajax({
url: "../TotalManager/User/SavePassword", //<span style="font-family:KaiTi_GB2312;">调用Controller中的方法将密码保存到Session中</span>
data: { password: strPassword },
type: "post",
success: function (text) {
$("#signupForm").submit(); <span style="font-family:KaiTi_GB2312;">//将表单进行提交</span>
}
});
}
else {
fnPosTri(); //输入内容不合法进行<span style="font-family:KaiTi_GB2312;">提示</span>
}
}
3.Ajax调用controller中的SavePassword方法(供系统之间跳转核实用):
<span style="font-family:KaiTi_GB2312;"> </span> /// <summary>
/// 将输入的密码保存在Session中
/// </summary>
/// <returns></returns>
public JsonResult SavePassword()
{
string strPasswrod = Request["password"].ToString();
Session["Password"] = strPasswrod;
return Json("", JsonRequestBehavior.AllowGet);
}
4.存储Session成功后通过Submit方法将表单提交后,执行的PIMS.RP.UI.DLL中的方法[注此DLL是另外项目发布形成]:
/// <summary>
/// 登录
/// </summary>
/// <returns></returns>
public ActionResult Login()
{
//获取用户名<span style="font-family:KaiTi_GB2312;">、</span>密码和验证码
string userName = Request.QueryString["us"] == null ? "" : Request.QueryString["us"];
string password = Request.QueryString["up"] == null ? "" : Request.QueryString["up"];
string code = Request.QueryString["code"] == null ? "" : Request.QueryString["code"];
string sCode = string.Empty;
if (Session["ValidateCode"] != null)
{
sCode = Session["ValidateCode"].ToString();
}
bool flag = true;
if (code == sCode)
flag = false;
if (flag)
{
return View("Login");
}
//验证用户输入
userName = ValidateMember.ValidateUserInputString(userName);
password = ValidateMember.ValidateUserInputString(password);
string sysId = ConfigurationManager.AppSettings["SysId"];
UpUsersComm user = PermissionManager.Instance.LoginVerify(userName, password, sysId);
//判断用户是否登录
if (user != null)
{
PlatformContext.Current[Session.SessionID] = user;
HttpContext.Session["userName"] = userName;
Session["ValidateCode"] = null;
Session["realName"] = user.RealName;
FormsAuthentication.SetAuthCookie(userName, false);
return RedirectToAction("Index", "Home");
}
else
{
log.Error("用户名或密码无效"+userName+""+password);
ViewBag.userName = string.IsNullOrEmpty(userName) ? "" : userName;
return View();
}
}
温馨提示:在这个过程中我一直有个疑问,View中能直接调引用DLL中的方法吗?在网上找到个案例证明打消我的疑问:http://www.cnblogs.com/sunjie9606/p/4329815.html
【总结】
1.经过这么个流程将登录整个流程给搞懂了!
2.发现一种新的方式,View能对应引用DLL中的Controller。
3.更加深刻体会到编程的乐趣。