登陆页面01

控制器:
using System;
usingSystem.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
usingMySchoolManageSystem2020.Models;
using System.Diagnostics;
namespaceMySchoolManageSystem2020.Controllers
{
publicclassMyMainController : Controller
{
// GET: MyMain
///<summary>
///学院信息管理系统=》登录、主页面
///</summary>
///<returns></returns>
publicActionResult Index()
{
return View();
}
//实例化
SchoolManageSystemEntities2 myModel = new Models.SchoolManageSystemEntities2();
#region 页面
///<summary>
///登陆页面
///</summary>
///<returns></returns>
publicActionResult MyLogin()
{
string UserNumber = "";
string Password = "";
bool isRemember = false;
//判断cookie是否有数据
HttpCookie cookie =System.Web.HttpContext.Current.Request.Cookies["user"];
if (cookie != null) {
if (cookie["userNumber"] != null) {
UserNumber = System.Web.HttpContext.Current.Server.UrlDecode(cookie["userNumber"]);
}
if (cookie["Password"] != null)
{
Password = System.Web.HttpContext.Current.Server.UrlDecode(cookie["Password"]);
}
isRemember = true;
}
//ViewBag
ViewBag.UserNumber = UserNumber;
ViewBag.PassWord = Password;
ViewBag.IsRemember = isRemember;
return View();
}
///<summary>
///主页面
///</summary>
///<returns></returns>
publicActionResult MyaMain()
{
return View();
}
#endregion
///<summary>
///生成验证码
///</summary>
///<returns></returns>
publicActionResult ValideCode()
{
//生成随机长度的字符串
string strValideCode =Common.ValidCodeUtils.GetRandomCode(5);
//Session 将生成的验证码保存到Session中
Session["ValideCode"] = strValideCode;
//将生成的字符串转化成图片 二进制数组变量接收
byte[] ImgValideCode =Common.ValidCodeUtils.CreateImage(strValideCode);
//返回视图层
return File(ImgValideCode,"image/jpeg");
}
publicActionResult DoCheckLogin()
{
string returnJson = "";
//Request[]接收参数
string strUserNumber =Request["UserNuber"];//用户名
string strPassword =Request["Password"];//密码
string strValideCode =Request["ValideCode"];//验证码
string strRememberMe =Request["RememberMe"];//记住否
//验证登录:验证码->账号验证->密码验证
if (Session["ValideCode"] != null) {
//提取验证码
string ValideCode =Session["ValideCode"].ToString().Trim();
if(strValideCode.Equals(ValideCode, StringComparison.CurrentCultureIgnoreCase))
{
try
{
//验证用户是否存在
//linq单表查询写法
/*
(from 自定义的表名 in Model对象,查询的表
【where自定义的表名.字段1 关系运算符 值 【&& 自定义的表明.字段2关系运算符 值】】
【orderby自定义的表名.字段】
【select自定义的表名】
匿名对象
【selectnew {
自定义的表名.字段1,
自定义的表名.字段2,
自定义的表名.字段3,
....
}】
【selectnew 类名{
类的属性1=自定义的表名.字段1,
类的属性2=自定义的表名.字段2,
....
}】
).Single();//查询单条数据,当没有数据或者有多条数据时会触发异常
.ToList();//查询多条数据并转为List
.Count();//查询有多少条数据
*/
var LinqUser = (from tbUser in myModel.PW_User
where tbUser.UserNuber ==strUserNumber.Trim()
select tbUser).Single();//查询返回的数据有且仅有一条
//密码验证
string AESPassword =Common.AESEncryptHelper.Encrypt(strPassword);
if(AESPassword.Equals(LinqUser.Password.Trim()))
{
//returnJson ="success";
//判断是否勾选了“记住否”
if (strRememberMe == "true")
{
//使用cookie记录用户的登录信息
//创建cookie对象
HttpCookie cookie = newHttpCookie("user");
cookie["userNumber"] = strUserNumber;
cookie["Password"] = strPassword;
//设置期限(7天)
cookie.Expires= DateTime.Now.AddDays(7);
//将cookie添加到请求集合中
Response.Cookies.Add(cookie);
}
else {
//创建cookie对象
HttpCookie cookie = newHttpCookie("user");
//设置期限(前一天)
cookie.Expires= DateTime.Now.AddDays(-1);
//将cookie添加到请求集合中
Response.Cookies.Add(cookie);
}
returnJson = "success";
}
else {
returnJson = "用户密码错误!";
}
}
catch (Exception e) {
Debug.WriteLine(e);
returnJson = "用户名错误!";
}
}
else
{
returnJson = "验证码输入错误,请检查!";
}
}
return Json(returnJson,JsonRequestBehavior.AllowGet);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值