C#、.Net MVC 三层架构, 实现连接SQL 数据库登录验证

对于一个初学者,或者第一次进行实战开发的时候;可能很多人都不知道从何下手;当初我也是这样:很是是渴望有个可以参考的源码例子!!!!!!

以下分享使用.net 标准的MVC 结构,EF缓存技术连接SQL数据库,界面Bootstrap;实现登录页面跳转主页面实例;也可以根据其结构添加代码,扩展功能;
1、项目截图
C#、.Net MVC 三层架构, 实现连接SQL 数据库登录验证_第1张图片
(1)用户表实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApp.Models.Domain
{
    /// 
    ///  sysUserInfo    
    /// 
    public class sysUserInfo
    {
        public sysUserInfo()
        {
            this.Id = 0;
            this.Account = string.Empty;
            this.RealName = string.Empty;
            this.Password = string.Empty;
            this.Remarks = string.Empty;
            this.College = string.Empty;
            this.Class = string.Empty;
            this.Mail = string.Empty;
            this.Sex = true;
            this.IsEnable = false;
            this.CreateMan = string.Empty;
            this.CreateDate = DateTime.Now.Date;
        }
        public int Id { get; set; }
        /// 
        /// 账号 
        /// 
        public string Account { get; set; }

        /// 
        /// 真实姓名    
        /// 
        public string RealName { get; set; }

        /// 
        /// 密码    
        /// 
        public string Password { get; set; }

        /// 
        /// 性别
        /// 
        public bool Sex { get; set; }

        /// 
        /// 备注    
        /// 
        public string Remarks { get; set; }

        /// 
        /// 学院    
        /// 
        public string College { get; set; }

        /// 
        /// 班级    
        /// 
        public string Class { get; set; }

        /// 
        /// 邮件    
        /// 
        public string Mail { get; set; }

        /// 
        /// 账户是否可用
        /// 
        public bool IsEnable { get; set; }
        /// 
        /// 创建人
        /// 

        public string CreateMan { get; set; }
        /// 
        /// 创建人
        /// 
        public DateTime CreateDate { get; set; }

    }

}

(2)用户表映射类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApp.Models.Mapping
{
    public class sysUserInfoMap : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Domain.sysUserInfo>
    {
        public sysUserInfoMap()
        {
            ToTable("sysUserInfo", "dbo");
            HasKey(t => t.Id);

            Property(t => t.Id).HasColumnName("Id").IsRequired();
            Property(t => t.Account).HasColumnName("Account").HasMaxLength(50).IsRequired();
            Property(t => t.RealName).HasColumnName("RealName").HasMaxLength(50).IsRequired();
            Property(t => t.Password).HasColumnName("Password").HasMaxLength(50).IsRequired();
            Property(t => t.Remarks).HasColumnName("Remarks").HasMaxLength(50).IsRequired();
            Property(t => t.College).HasColumnName("College").HasMaxLength(50).IsRequired();
            Property(t => t.Class).HasColumnName("Class").HasMaxLength(50).IsRequired();
            Property(t => t.Mail).HasColumnName("Mail").HasMaxLength(50).IsRequired();
            Property(t => t.Sex).HasColumnName("Sex").IsRequired();
            Property(t => t.IsEnable).HasColumnName("IsEnable").IsRequired();
            //Property(t => t.CreateMan).HasColumnName("CreateMan").HasMaxLength(50).IsRequired();
            Property(t => t.CreateDate).HasColumnName("CreateDate").IsRequired();


        }
    }
}

(3)用户信息查询类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using WebApp.Models.Domain;

namespace WebApp.Application
{
    public class UserInfoApp
    {
        public WebDBContext dbcontext = new WebDBContext();

        /// 
        /// 查所有用户
        /// 
        /// 
        public IQueryable<sysUserInfo> IQueryable()
        {
            return dbcontext.Set<sysUserInfo>();
        }
        /// 
        /// 根据条件查列表用户
        /// 
        /// 
        /// 
        public IQueryable<sysUserInfo> IQueryable(Expression<Func<sysUserInfo, bool>> condition)
        {
            return dbcontext.Set<sysUserInfo>().Where(condition);
        }

        /// 
        /// 根据条件查用户
        /// 
        /// 
        /// 
        public sysUserInfo FindEntity(Expression<Func<sysUserInfo, bool>> condition)
        {
            return dbcontext.Set<sysUserInfo>().FirstOrDefault(condition);
        }
    }
}

2、登录页面
C#、.Net MVC 三层架构, 实现连接SQL 数据库登录验证_第2张图片
(1)登录页面引用

    <link  rel="stylesheet" href="~/Content/css/bootstrap.min.css">
    <script src="~/Content/js/jquery-1.10.2.min.js"></script>
    <link  rel="stylesheet" href="~/Content/css/LFrame/loginUI.css">

    <script src="~/Content/UserJS/login.js"></script>

(2)自定义login.js;(能实现用户验证跳转,但是比较简单,待完善)

$(function () {

     $("#bt_Login").click( function () {
         var Account = $("#Account").val();
         if (Account=="")
         {
             SetMsg("用户不能为空!")
             return false;
         }
         var password = $("#Password").val();

         if (password=="")
         {
             SetMsg("密码不能为空!")
             return false;
         }
         //var $code = $("#VarCode");

         $.ajax({
             url: "/Login/CheckLogin",
             data: { Account: Account, password: password},
             type: "post",
             dataType: "json",
             success: function (data) {
                 if (data.state == 1) {

                     window.location.href = "/Home/Index";
                     //$("#login_button").find('span').html("登录成功,正在跳转...");
                     //window.setTimeout(function () {
                     //    window.location.href = "/Home/Index";
                     //}, 500);
                 } else {
                     $("#Password").val('');
                     SetMsg(data.message);
                 }
             }
         });
     });

     $("#bt_Reset").click(function () {
         $("#Account").val('');
         $("#Password").val('');
         $("#Account").focus();

     });


     function SetMsg(msg) {
         document.getElementById('tips_msg').innerText = msg;
     };
 });

3、系统主页面
C#、.Net MVC 三层架构, 实现连接SQL 数据库登录验证_第3张图片

4、下载:源码下载地址

你可能感兴趣的:(.net,+,BootStrap网页框架)