ASP.NET MVC4 用户登录验证



文件结构:





主要核心代码:
View/AdminLogin代码:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <meta name="viewport" content="width=device-width" />
    <title>AdminLogin</title>
    <link href="../../css/body.css" rel="stylesheet" />
    <link href="../../css/style.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <section id="content">
            <form method="post" action="/SysAdmin/AdminLogin">
                <h1>OpenJudge</h1>
                <div>
                    <input type="text" placeholder="用户名" required="" id="username" name="id" />
                </div>
                <div>
                    <input type="password" placeholder="密码" required="" id="password" name="loginpwd" />
                </div>
                <div class="">
                    <span class="help-block u-errormessage" id="js-server-helpinfo"> </span>
                </div>
                <div>
                    <!-- <input type="submit" value="Log in" /> -->
                    <input type="submit" value="登录" class="btn btn-primary" id="js-btn-login" />
                    <a href="#">忘记密码?</a>
                    <!-- <a href="#">Register</a> -->
                </div>
            </form>
            <%=ViewData["info"] %>
            <!-- form -->
            <!-- button -->
        </section>
        <!-- content -->
    </div>
    <!-- container -->
    <div style="text-align: center; margin: 10%">
        <p>©2015 EF <a href="http://www.efan.in" target="_blank">逸凡软件</a></p>
    </div>
</body>
</html>





SysAdminController代码:



using MvcLogin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace MvcLogin.Controllers
{
    public class SysAdminController : Controller
    {
        //
        // GET: /SysAdmin/


        public ActionResult Index()
        {
            //AdminLogin不是下边方法的名,而是Views文件夹下SysAdmin文件夹下的AdminLogin.aspx视图的名AdminLogin
            return View("AdminLogin");
        }
        /// <summary>
        /// 返回用户请求数据结果
        /// </summary>
        /// <returns></returns>
        public ActionResult AdminLogin()
        {
            //[1]获取用户输入数据
            SysAdmin objAdmin = new SysAdmin() 
            { 
                id=Convert.ToInt32(Request.Params["id"]),
                loginpwd = Request.Params["loginpwd"]               
            };
            //[2]f调用模型与Model交互
            objAdmin = new SysAdminService().AdminLogin(objAdmin);
            if (objAdmin != null)
            {
                ViewData["info"] = "欢迎您:" + objAdmin.adminname;
            }
            else
            {
                ViewData["info"] = "警告:用户名和密码错误" ;
            }
            //【3】返回视图
            //默认返回函数名同名视图即返回
            return View();
        }


    }
}




Modol代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;




namespace MvcLogin.Models
{
    public class SysAdminService
    {
        /// <summary>
        /// 根据用户名和密码查询
        /// </summary>
        /// <returns>SysAdmin</returns>
        public SysAdmin AdminLogin(SysAdmin objAdmin) 
        {
            string sql = string.Format("select adminname from  tb_user where id={0} and loginpwd={1}",objAdmin.id, objAdmin.loginpwd);
            SqlDataReader objReader = SQLHelp.GetReader(sql);
            if (objReader.Read())
            {
                objAdmin.adminname = objReader["adminname"].ToString();
            }
            else
            {
                //如果用户名和密码不正确时则清空当前对象
                objAdmin = null;
            }
            //关闭连接
            objReader.Close();
            return objAdmin;
        }
    }
}



SQLHelper代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using  System.Data.SqlClient;
using System.Data;




namespace MvcLogin.Models
{
    public class SQLHelp
    {
        private static string connString = ConfigurationManager.ConnectionStrings["connString"].ToString();
        /// <summary>
        /// 返回结果集
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static SqlDataReader GetReader(string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(sql,conn);
            try
            {
                conn.Open();
                return cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                return null;              
            }
        }
    }
}




SysAdmin代码:


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




namespace MvcLogin.Models
{
    public class SysAdmin
    {
        public int id { get; set; }
        public string adminname { get; set; }
        public string loginpwd { get; set; }
    }
}



核心代码大概就这些,具体如果有疑问的可以追问,希望会帮助到您。







已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页