原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录

我们之前做了验证码,登录界面,却没有登录实际的代码,我们这次先把用户登录先完成了,要不权限是讲不下去了

把我们之前的表更新到EF中去

登录在Account控制器,所以我们要添加Account的Model,BLL,DAL

AccountModel我们已经创建好了,下面是DAL和BLL的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Models; namespace App.IDAL
{
public interface IAccountRepository
{
SysUser Login(string username, string pwd);
}
}

IAccountRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Models;
using App.IDAL; namespace App.DAL
{
public class AccountRepository : IAccountRepository,IDisposable
{
public SysUser Login(string username, string pwd)
{
using (DBContainer db = new DBContainer())
{
SysUser user = db.SysUser.SingleOrDefault(a => a.UserName == username && a.Password == pwd);
return user;
}
}
public void Dispose()
{ }
}
}

AccountRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Models; namespace App.IBLL
{
public interface IAccountBLL
{
SysUser Login(string username, string pwd);
}
}

IAccountBLL

using System.Linq;
using System.Text;
using App.IBLL;
using App.BLL.Core;
using Microsoft.Practices.Unity;
using App.IDAL;
using App.Models;
using App.Common;
namespace App.BLL
{
public class AccountBLL:BaseBLL,IAccountBLL
{
[Dependency]
public IAccountRepository accountRepository { get; set; }
public SysUser Login(string username, string pwd)
{
return accountRepository.Login(username, pwd); }
}
}

AccountBLL

注入到容器

 container.RegisterType<IAccountBLL, AccountBLL>();
container.RegisterType<IAccountRepository, AccountRepository>();

然后回到Account的控制器上

定义

[Dependency]
public IAccountBLL accountBLL { get; set; }

在 public JsonResult Login(string UserName, string Password, string Code)

方法下添加代码

  if (Session["Code"] == null)
return Json(JsonHandler.CreateMessage(, "请重新刷新验证码"), JsonRequestBehavior.AllowGet); if (Session["Code"].ToString().ToLower() != Code.ToLower())
return Json(JsonHandler.CreateMessage(, "验证码错误"), JsonRequestBehavior.AllowGet);
SysUser user = accountBLL.Login(UserName, ValueConvert.MD5(Password));
if (user == null)
{
return Json(JsonHandler.CreateMessage(, "用户名或密码错误"), JsonRequestBehavior.AllowGet);
}
else if (!Convert.ToBoolean(user.State))//被禁用
{
return Json(JsonHandler.CreateMessage(, "账户被系统禁用"), JsonRequestBehavior.AllowGet);
} AccountModel account = new AccountModel();
account.Id = user.Id;
account.TrueName = user.TrueName;
Session["Account"] = account; return Json(JsonHandler.CreateMessage(, ""), JsonRequestBehavior.AllowGet);

其中用到一个加密类处理,这里用的是一个MD5大家可以用自己的加密方式

然而这个类里面包含了其他的一些字符串处理,算是在这里共享给大家。不合适就删掉了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
namespace YmNets.Common
{
public static partial class ValueConvert
{
/// <summary>
/// 使用MD5加密字符串
/// </summary>
/// <param name="str">待加密的字符</param>
/// <returns></returns>
public static string MD5(this string str)
{
if (string.IsNullOrEmpty(str))
{
return string.Empty;
}
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] arr = UTF8Encoding.Default.GetBytes(str);
byte[] bytes = md5.ComputeHash(arr);
str = BitConverter.ToString(bytes);
//str = str.Replace("-", "");
return str;
}
/// <summary>
/// 将最后一个字符串的路径path替换
/// </summary>
/// <param name="str"></param>
/// <param name="path"></param>
/// <returns></returns>
public static string Path(this string str, string path)
{
int index = str.LastIndexOf('\\');
int indexDian = str.LastIndexOf('.');
return str.Substring(, index + ) + path + str.Substring(indexDian);
}
public static List<string> ToList(this string ids)
{
List<string> listId = new List<string>();
if (!string.IsNullOrEmpty(ids))
{
var sort = new SortedSet<string>(ids.Split(','));
foreach (var item in sort)
{
listId.Add(item); }
}
return listId;
}
/// <summary>
/// 从^分割的字符串中获取多个Id,先是用 ^ 分割,再使用 & 分割
/// </summary>
/// <param name="ids">先是用 ^ 分割,再使用 & 分割</param>
/// <returns></returns>
public static List<string> GetIdSort(this string ids)
{
List<string> listId = new List<string>();
if (!string.IsNullOrEmpty(ids))
{
var sort = new SortedSet<string>(ids.Split('^')
.Where(w => !string.IsNullOrWhiteSpace(w) && w.Contains('&'))
.Select(s => s.Substring(, s.IndexOf('&'))));
foreach (var item in sort)
{
listId.Add(item);
}
}
return listId;
}
/// <summary>
/// 从,分割的字符串中获取单个Id
/// </summary>
/// <param name="ids"></param>
/// <returns></returns>
public static string GetId(this string ids)
{
if (!string.IsNullOrEmpty(ids))
{
var sort = new SortedSet<string>(ids.Split('^')
.Where(w => !string.IsNullOrWhiteSpace(w) && w.Contains('&'))
.Select(s => s.Substring(, s.IndexOf('&'))));
foreach (var item in sort)
{
if (!string.IsNullOrWhiteSpace(item))
{
return item;
}
}
}
return null;
}
/// <summary>
/// 将String转换为Dictionary类型,过滤掉为空的值,首先 6 分割,再 7 分割
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static Dictionary<string, string> StringToDictionary(string value)
{
Dictionary<string, string> queryDictionary = new Dictionary<string, string>();
string[] s = value.Split('^');
for (int i = ; i < s.Length; i++)
{
if (!string.IsNullOrWhiteSpace(s[i]) && !s[i].Contains("undefined"))
{
var ss = s[i].Split('&');
if ((!string.IsNullOrEmpty(ss[])) && (!string.IsNullOrEmpty(ss[])))
{
queryDictionary.Add(ss[], ss[]);
}
} }
return queryDictionary;
}
/// <summary>
/// 得到对象的 Int 类型的值,默认值0
/// </summary>
/// <param name="Value">要转换的值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值0</returns>
public static int GetInt(this object Value)
{
return GetInt(Value, );
}
/// <summary>
/// 得到对象的 Int 类型的值,默认值0
/// </summary>
/// <param name="Value">要转换的值</param>
/// <param name="defaultValue">如果转换失败,返回的默认值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值0</returns>
public static int GetInt(this object Value, int defaultValue)
{ if (Value == null) return defaultValue;
if (Value is string && Value.GetString().HasValue() == false) return defaultValue; if (Value is DBNull) return defaultValue; if ((Value is string) == false && (Value is IConvertible) == true)
{
return (Value as IConvertible).ToInt32(CultureInfo.CurrentCulture);
} int retVal = defaultValue;
if (int.TryParse(Value.ToString(), NumberStyles.Any, CultureInfo.CurrentCulture, out retVal))
{
return retVal;
}
else
{
return defaultValue;
}
}
/// <summary>
/// 得到对象的 String 类型的值,默认值string.Empty
/// </summary>
/// <param name="Value">要转换的值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值string.Empty</returns>
public static string GetString(this object Value)
{
return GetString(Value, string.Empty);
}
/// <summary>
/// 得到对象的 String 类型的值,默认值string.Empty
/// </summary>
/// <param name="Value">要转换的值</param>
/// <param name="defaultValue">如果转换失败,返回的默认值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值 。</returns>
public static string GetString(this object Value, string defaultValue)
{
if (Value == null) return defaultValue;
string retVal = defaultValue;
try
{
var strValue = Value as string;
if (strValue != null)
{
return strValue;
} char[] chrs = Value as char[];
if (chrs != null)
{
return new string(chrs);
} retVal = Value.ToString();
}
catch
{
return defaultValue;
}
return retVal;
}
/// <summary>
/// 得到对象的 DateTime 类型的值,默认值为DateTime.MinValue
/// </summary>
/// <param name="Value">要转换的值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回的默认值为DateTime.MinValue </returns>
public static DateTime GetDateTime(this object Value)
{
return GetDateTime(Value, DateTime.MinValue);
} /// <summary>
/// 得到对象的 DateTime 类型的值,默认值为DateTime.MinValue
/// </summary>
/// <param name="Value">要转换的值</param>
/// <param name="defaultValue">如果转换失败,返回默认值为DateTime.MinValue</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回的默认值为DateTime.MinValue</returns>
public static DateTime GetDateTime(this object Value, DateTime defaultValue)
{
if (Value == null) return defaultValue; if (Value is DBNull) return defaultValue; string strValue = Value as string;
if (strValue == null && (Value is IConvertible))
{
return (Value as IConvertible).ToDateTime(CultureInfo.CurrentCulture);
}
if (strValue != null)
{
strValue = strValue
.Replace("年", "-")
.Replace("月", "-")
.Replace("日", "-")
.Replace("点", ":")
.Replace("时", ":")
.Replace("分", ":")
.Replace("秒", ":")
;
}
DateTime dt = defaultValue;
if (DateTime.TryParse(Value.GetString(), out dt))
{
return dt;
} return defaultValue;
}
/// <summary>
/// 得到对象的布尔类型的值,默认值false
/// </summary>
/// <param name="Value">要转换的值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值false</returns>
public static bool GetBool(this object Value)
{
return GetBool(Value, false);
} /// <summary>
/// 得到对象的 Bool 类型的值,默认值false
/// </summary>
/// <param name="Value">要转换的值</param>
/// <param name="defaultValue">如果转换失败,返回的默认值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值false</returns>
public static bool GetBool(this object Value, bool defaultValue)
{
if (Value == null) return defaultValue;
if (Value is string && Value.GetString().HasValue() == false) return defaultValue; if ((Value is string) == false && (Value is IConvertible) == true)
{
if (Value is DBNull) return defaultValue; try
{
return (Value as IConvertible).ToBoolean(CultureInfo.CurrentCulture);
}
catch { }
} if (Value is string)
{
if (Value.GetString() == "") return false;
if (Value.GetString() == "") return true;
if (Value.GetString().ToLower() == "yes") return true;
if (Value.GetString().ToLower() == "no") return false;
}
/// if (Value.GetInt(0) != 0) return true;
bool retVal = defaultValue;
if (bool.TryParse(Value.GetString(), out retVal))
{
return retVal;
}
else return defaultValue;
}
/// <summary>
/// 检测 GuidValue 是否包含有效的值,默认值Guid.Empty
/// </summary>
/// <param name="GuidValue">要转换的值</param>
/// <returns>如果对象的值可正确返回, 返回对象转换的值 ,否则, 返回默认值Guid.Empty</returns>
public static Guid GetGuid(string GuidValue)
{
try
{
return new Guid(GuidValue);
}
catch { return Guid.Empty; }
}
/// <summary>
/// 检测 Value 是否包含有效的值,默认值false
/// </summary>
/// <param name="Value"> 传入的值</param>
/// <returns> 包含,返回true,不包含,返回默认值false</returns>
public static bool HasValue(this string Value)
{
if (Value != null)
{
return !string.IsNullOrEmpty(Value.ToString());
}
else return false;
} }
}

ValueConvert.cs

回到前端把alert(1);替换以下代码

 $.post('/Account/Login', { UserName: $("#UserName").val(), Password: $("#Password").val(), Code: $("#ValidateCode").val() },
function (data) { if (data.type == "1") {
window.location = "/Home/Index"
} else {
$("#mes").html(data.message);
}
$("#Loading").hide();
}, "json");
return false;

可以登录了,大家试一下吧!帐号admin,密码admin123

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(19)-权限管理系统-用户登录的更多相关文章

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

  2. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充 系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支 系列目录 步骤设置完毕之后,就要设置好流转了,比如财务申请大于50000元( ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-【过滤器+Cache】

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-[过滤器+Cache] 系列目录 上次的探讨没有任何结果,我浏览了大量的文章 ...

  10. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...

随机推荐

  1. [Machine Learning] Probabilistic Graphical Models:一、Introduction and Overview(2、Factors)

    一.什么是factors? 类似于function,将一个自变量空间投影到新空间.这个自变量空间叫做scope. 二.例子 如概率论中的联合分布,就是将不同变量值的组合映射到一个概率,概率和为1. 三 ...

  2. qrcode.js插件将你的内容转换成二维码格式

    ---qrcode.js插件将你的内容转换成二维码格式--- 我之前一直想知道二维码是怎么生成,所以就了解了一下, 最后还是不知道它的原理, 但是,我知道怎么生成. 现在就让我带你制作一个你喜爱的二维 ...

  3. ExecuteNonQuery()返回值

    查询某个表中是否有数据的时候,我用了ExecuteNonQuery(),并通过判断值是否大于0来判断数据的存在与否.结果与我所设想的很不一致,调试时才发现,其执行后返回的结果是-1,对此我很是不理解, ...

  4. 配置nginx支持thinkphp框架

    因为nginx本身没有支持pathinfo,所以无法使用thinkphp框架,不过我们可以在配置里进行修改使其能够正常使用thinkphp. 1.修改配置支持pathinfo vi /etc/ngin ...

  5. 【转】TypeScript中文入门教程

    目录 虽然我是转载的,但看在Copy这么多文章也是很幸苦的好吧,我罗列一个目录. 转载:<TypeScript 中文入门教程> 17.注解 (2015-12-03 11:36) 转载:&l ...

  6. Java日志性能

    在任何系统中,日志都是非常重要的组成部分,它是反映系统运行情况的重要依据,也是排查问题时的必要线索.绝大多数人都认可日志的重要性,但是又有多少人仔细想过该怎么打日志,日志对性能的影响究竟有多大呢?今天 ...

  7. Android程序的隐藏与退出

    转自Android程序的隐藏与退出 Android的程序无需刻意的去退出,当你一按下手机的back键的时候,系统会默认调用程序栈中最上层Activity的Destroy()方法来销毁当前Activit ...

  8. rest开发

    http://www.mkyong.com/webservices/jax-rs/download-json-from-jax-rs-with-jaxb-resteasy/ http://blog.j ...

  9. Android 两个Activity进行数据传送 发送

    Activity1:: Intent intent= new Intent(this, OtherActivity.class); String name = "heyiyong" ...

  10. DLL ActiveForm 线程同步问题

    本文试着从分析Synchronize同步执行的实现机制入手,来解决DLL/ActiveForm中线程同步的问题. 线程中进行同步时调用的Synchronize函数,仅仅是把调用调用线程.调用方法地址. ...