您的位置:首页 > 编程语言 > ASP

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 (16)源码分享二:登录功能以及UI、数据库、仓储源码分享

2016-08-20 10:13 1096 查看

索引

  

【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目 目录索引

  

前言

  

  前面还没有下载到UI和数据库的,这里再次给大家提供一下:百度网盘 提取码:fuuv ,UI是参照H+,但是H+是收费授权的(¥998RMB),价格有点贵,所以 我们的 UI 跟H+ 虽然是一个风格,但是大家可以看到,CSS基本用的都是Bootstrap,JS 基本都是 最新的开源插件和自己重写的,不存在版权问题。

  我们前一章已经重写了我们的仓储类,文章中接口都已列出,实现类也说的很明白,引入 uow 后,大家修改一下原先的提交,就可以。但是很多朋友 总感觉 不给你文件 是因为 有些东西没有放给你,那么 就再次给大家 下载一下 我的仓储,大家可以对比一下,是不是博客中故意少些东西。修改后的仓储:百度网盘 提取码:qw64

其实,只是想让大家自己动手一下,修改方法很简单,就是把我们之前的方法加上个条件: bool IsCommit=true,当我们只进行一项操作的时候,默认直接提交,当我们进行多项操作的时候,可以传入false,不提交,然后通过uow 统一提交。大家可以看一下,使用的例子:

  

using Common;
using Service.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebPage.Areas.SysManage.Controllers
{
public class AccountController : Controller
{
#region 声明容器
/// <summary>
/// 用户管理
/// add yuangang by 2016-05-16
/// </summary>
IUserManage UserManage { get; set; }
/// <summary>
/// 用户在线管理
/// </summary>
IUserOnlineManage UserOnlineManage { get; set; }
/// <summary>
/// 日志记录
/// </summary>
log4net.Ext.IExtLog log = log4net.Ext.ExtLogManager.GetLogger("dblog");
#endregion

#region 基本视图
public ActionResult Index()
{
//移除Session
SessionHelper.Remove("CurrentUser");
CookieHelper.ClearCookie("cookie_rememberme");
return View();
}
/// <summary>
/// 登录验证
/// add yuangang by 2016-05-16
/// </summary>
[ValidateAntiForgeryToken]
public ActionResult Login(Domain.SYS_USER item)
{
var json = new JsonHelper() { Msg = "登录成功", Status = "n" };
try
{
//获取表单验证码
var code = Request.Form["code"];
if (Session["gif"] != null)
{
//判断用户输入的验证码是否正确
if (!string.IsNullOrEmpty(code) && code.ToLower() == Session["gif"].ToString().ToLower())
{
//调用登录验证接口 返回用户实体类
var users = UserManage.UserLogin(item.ACCOUNT.Trim(), item.PASSWORD.Trim());
if (users != null)
{
//是否锁定
if (users.ISCANLOGIN)
{
json.Msg = "用户已锁定,禁止登录,请联系管理员进行解锁";
log.Warn(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
return Json(json);
}

var acconut = this.UserManage.GetAccountByUser(users);

//系统访问正常
if (acconut.System_Id.Count > 0)
{
//是否启用单用户登录
if (System.Configuration.ConfigurationManager.AppSettings["IsSingleLogin"] == "True" )
{
var UserOnline = UserOnlineManage.LoadListAll(p => p.FK_UserId == users.ID).FirstOrDefault();
if(UserOnline!=null && UserOnline.IsOnline)
{
json.Msg = "当前用户已登录,系统不允许重复登录!登录IP:" + UserOnline.UserIP;
log.Error(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "重复登录:" + json.Msg);
}
else
{
//写入Session 当前登录用户
SessionHelper.SetSession("CurrentUser", acconut);

//记录用户信息到Cookies
string cookievalue = "{\"id\":\"" + acconut.Id + "\",\"username\":\"" + acconut.LogName +
"\",\"password\":\"" + acconut.PassWord + "\",\"ToKen\":\"" +
Session.SessionID + "\"}";
CookieHelper.SetCookie("cookie_rememberme", new Common.CryptHelper.AESCrypt().Encrypt(cookievalue),
null);

json.Status = "y";
json.ReUrl = "/Sys/Home/Index";
log.Info(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
}
}
else
{
//写入Session 当前登录用户
SessionHelper.SetSession("CurrentUser", acconut);

//记录用户信息到Cookies
string cookievalue = "{\"id\":\"" + acconut.Id + "\",\"username\":\"" + acconut.LogName +
"\",\"password\":\"" + acconut.PassWord + "\",\"ToKen\":\"" +
Session.SessionID + "\"}";
CookieHelper.SetCookie("cookie_rememberme", new Common.CryptHelper.AESCrypt().Encrypt(cookievalue),
null);

json.Status = "y";
json.ReUrl = "/Sys/Home/Index";
log.Info(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
}
}
else
{
json.Msg = "站点来源不可信,系统拒绝登录";
log.Warn(Utils.GetIP(), "其他系统访问者", "", "Login", "其他系统登录失败,原因:系统验证错误,系统拒绝登录");
}

}
else
{
json.Msg = "用户名或密码不正确";
log.Error(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
}
}
else
{
json.Msg = "验证码不正确";
log.Error(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
}
}
else
{
json.Msg = "验证码已过期,请刷新验证码";
log.Error(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
}
}
catch (Exception e)
{
json.Msg = e.Message;
log.Error(Utils.GetIP(), item.ACCOUNT, Request.Url.ToString(), "Login", "系统登录,登录结果:" + json.Msg);
}
return Json(json, JsonRequestBehavior.AllowGet);
}
#endregion

#region 帮助方法
/// <summary>
/// 验证码
/// </summary>
public FileContentResult ValidateCode()
{
string code = "";
System.IO.MemoryStream ms = new Models.verify_code().Create(out code);
Session["gif"] = code;//验证码存储在Session中,供验证。
Response.ClearContent();//清空输出流
return File(ms.ToArray(), @"image/png");
}
#endregion
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐