UI层一个登录框,一个密码框,一个验证码框 ,然后一个记住密码
<div class="banner">
<div class="login-aside">
<div id="o-box-up" style="height:400px"></div>
<div id="o-box-down" style="table-layout:fixed;height:400px;">
<div class="error-box"></div>
<form class="registerform" method="post" action="/shop/account/login">
<input type="hidden" value="@ViewBag.returnUrl" name="url" />
<div class="fm-item">
<label for="logonId" class="form-label">用户登录:</label>
@*<input type="text" value="请输入账号" maxlength="100" id="username" class="i-text" >*@
@Html.TextBoxFor(m => m.username, new {@class="i-text",@placeholder="请输入账号" })<br />
@Html.ValidationMessageFor(m=>m.username)
<div class="ui-form-explain"></div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label">输入密码:</label>
@*<input type="password" name="password" value="" maxlength="100" id="password" class="i-text" >*@
@Html.PasswordFor(m => m.password, new { @class="i-text",@placeholder="请输入密码"})<br />
@Html.ValidationMessageFor(m=>m.password)
<div class="ui-form-explain"></div>
</div>
<div class="fm-item pos-r">
<label for="logonId" class="form-label">验证码</label>
@*<input type="text" id="authInput" value="输入验证码" id="yzm" class="i-text yzm" >*@
@Html.TextBoxFor(m => m.authInput, new {@class="i-text yzm",@placeholder="请输入验证码" })
<div class="ui-form-explain"><img id="authCode" height="38" width="80" src="/shop/account/authCode" onclick="this.src=‘/shop/account/authCode?r=‘+Math.random()" class="yzm-img" /></div>
</div>
<div class="fm-item" style="padding-top:10px">
@Html.CheckBoxFor(m=>m.isRemember)
@*<input type="checkbox" />*@
<span class="remember" style="line-height: 12px;text-align: left;width: auto;font-size: 14px; color: #fff; font-weight: bold; ">记住密码</span>
<div class="findPass">
<a class="findpassLink" href="#" style="display:none;">忘记密码</a>
</div>
</div>
<div class="fm-item">
<label for="logonId" class="form-label">@Html.ValidationMessage("error")</label>
<input type="submit" value="登录" tabindex="4" id="send-btn" class="btn-login">
</div>
</form>
</div>
</div>
controller中第一个是登录,登录的时候验证是否勾选了记住密码 ,记住了就存入cookie
#region 登陆
/// <summary>
/// 登陆
/// </summary>
/// <param name="url">返回页面</param>
/// <returns></returns>
[HttpGet]
public ActionResult login(string url = "")
{
ViewBag.userInfo = getRUserInfo();
ViewBag.returnUrl = url;
return View();
}
/// <summary>
/// 执行登陆
/// </summary>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <param name="authCode"></param>
/// <param name="isRemember"></param>
/// <returns></returns>
[HttpPost]
public ActionResult login(shopModel model, string url)
{
string mess = "serverErr";
if (Session["auth"] != null)
{
if (ModelState.IsValid)
{
if (Session["auth"].ToString().ToLower() == model.authInput.ToLower())
{
//string md5_pass = getMD5Code(model.password);//密码
DataProtectionProxy.DataProtectionProxy dp = new DataProtectionProxy.DataProtectionProxy();
if (String.IsNullOrEmpty(model.password))
model.password = "";
else
model.password= dp.Encrypt(model.password);
Web.Model.WeiXin.User user = new AccountHandler().login(model.username, model.password);
if (user != null)
{
Session["wx_user"] = user;
if (model.isRemember)
{
addRUserInfo(new Web.Model.WeiXin.User() { loginname = model.username, password = model.password });
}
else
{
deleteRUserInfo();
}
if (string.IsNullOrEmpty(url))
{
return Redirect("/shop/order/shopcart");
}
else
{
return Redirect(url);
}
}
else
{
ModelState.AddModelError("error", "用户名或密码输入错误");
}
}
else
{
ModelState.AddModelError("error", "验证码输入错误");
}
}
}
ViewBag.returnUrl = url;
return View();
}
/// <summary>
/// 登出操作
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult logout()
{
Session.Remove("wx_user");
return Content("success");
}
#endregion
#region 修改记录用户信息
/// <summary>
/// 设置本地记录用户信息
/// </summary>
/// <param name="user"></param>
public void addRUserInfo(Web.Model.WeiXin.User user)
{
string userInfo = Newtonsoft.Json.JsonConvert.SerializeObject(user);
HttpCookie cookie = new HttpCookie("r_userInfo");
cookie.Value = HttpUtility.UrlEncode(userInfo, Encoding.UTF8);
cookie.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(cookie);
}
/// <summary>
/// 获取本机记录用户信息
/// </summary>
/// <returns></returns>
public Web.Model.WeiXin.User getRUserInfo()
{
Web.Model.WeiXin.User user = newWeb.Model.WeiXin.User();
if (Request.Cookies["r_userInfo"] != null)
{
JsonSerializer jserial = new JsonSerializer();
StringReader strread = new StringReader(HttpUtility.UrlDecode(Request.Cookies["r_userInfo"].Value, Encoding.UTF8));
user = jserial.Deserialize<TeenySoft.Web.Model.WeiXin.User>(new JsonTextReader(strread));
return user;
}
return null;
}
/// <summary>
/// 删除本机记录用户信息
/// </summary>
public void deleteRUserInfo()
{
if (Request.Cookies["r_userInfo"] != null)
{
Response.Cookies["r_userInfo"].Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Set(Response.Cookies["r_userInfo"]);
}
}
#endregion
#region 验证码
/// <summary>
/// 图片验证码
/// </summary>
public void authCode()
{
string authCode = getAuthCode();
Session["auth"] = authCode;
Response.ContentType = "image/jpeg";
createActhImg(authCode).Save(Response.OutputStream, ImageFormat.Jpeg);
}
/// <summary>
/// 生成验证码图片
/// </summary>
/// <param name="authCode"></param>
/// <returns></returns>
public Image createActhImg(string authCode)
{
Bitmap result = null;
//创建绘图
result = new Bitmap(authCode.Length * 16, 25);
using (Graphics objGraphics = Graphics.FromImage(result))
{
objGraphics.SmoothingMode = SmoothingMode.HighQuality;
//清除整个绘图面并以指定背景色填充
objGraphics.Clear(Color.AliceBlue);
//创建画笔
using (SolidBrush objSolidBrush = new SolidBrush(Color.Black))
{
this.AddForeNoisePoint(result);
this.AddBackgroundNoisePoint(result, objGraphics);
//文字居中
StringFormat objStringFormat = new StringFormat(StringFormatFlags.NoClip);
objStringFormat.Alignment = StringAlignment.Center;
objStringFormat.LineAlignment = StringAlignment.Center;
//字体样式
Font objFont = new Font(FontFamily.GenericSerif, objRandom.Next( 18- 3, 18), FontStyle.Regular);
//验证码旋转,防止机器识别
char[] chars = authCode.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
//转动的度数
float angle = objRandom.Next(-randomAngle,randomAngle);
objGraphics.TranslateTransform(12, 12);
objGraphics.RotateTransform(angle);
objGraphics.DrawString(chars[i].ToString(), objFont, objSolidBrush, -2, 2, objStringFormat);
objGraphics.RotateTransform(-angle);
objGraphics.TranslateTransform(2, -12);
}
}
}
return result;
}
/// <summary>
/// 添加前景噪点
/// </summary>
/// <param name="objBitmap"></param>
private void AddForeNoisePoint(Bitmap objBitmap)
{
for (int i = 0; i < objBitmap.Width * this.foreNoisePointCount; i++)
{
objBitmap.SetPixel(objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), Color.SeaGreen);
}
}
/// <summary>
/// 添加背景噪点
/// </summary>
/// <param name="objBitmap"></param>
/// <param name="objGraphics"></param>
private void AddBackgroundNoisePoint(Bitmap objBitmap, Graphics objGraphics)
{
using (Pen objPen = new Pen(Color.Azure, 0))
{
for (int i = 0; i < objBitmap.Width * 2; i++)
{
objGraphics.DrawRectangle(objPen, objRandom.Next(objBitmap.Width), objRandom.Next(objBitmap.Height), 1, 1);
}
}
}
/// <summary>
/// 生成验证码
/// </summary>
/// <returns></returns>
public string getAuthCode()
{
string authCode = "";
Random rand = new Random();
for (int i = 0; i < 4; i++)
{
int action = rand.Next(2);
switch (action)
{
case 0:
authCode += (char)rand.Next(48, 58);
break;
case 1:
authCode += (char)rand.Next(65, 91);
break;
}
}
return authCode;
}
/// <summary>
/// MD5编码
/// </summary>
/// <param name="pwd"></param>
/// <returns></returns>
public string getMD5Code(string pwd)
{
byte[] result = Encoding.Default.GetBytes(pwd); //tbPass为输入密码的文本框
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
return BitConverter.ToString(output).Replace("-", ""); //tbMd5pass为输出加密文本的文本框
}
#endregion