ASP.NET MVC 实现简单的登录 源码

1、创建一个控制器   如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Demo.Models;    //命名空间

namespace Demo.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        DL_DemoEntities db = new DL_DemoEntities();   //模型

        public ActionResult Index()  //首页
        {
            return View();
        }
        public ActionResult Demo404()  //登陆失败跳转
        {
            return View();
        }
        public ActionResult Start_Here()  //登陆成功跳转
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login(string name, string password) 
        {
             name = Request["name"];
             password = Request["password"];
            if (string .IsNullOrEmpty(name)||string .IsNullOrEmpty(password))
            {
                return Content("<script>alert('账号密码不能为空');location.href='Demo404'</script>");
                
            }
            else
            {
                var user = db.User.Where(u => u.admin == name && u.passid == password).SingleOrDefault();
                if (user == null)
                {
                    return Content("<script>alert('输入有误');location.href='Demo404'</script>");

                }
                else
                {
                    return Content("<script>location.href='Start_Here'</script>");
//登录成功跳转 } } } } }

2、视图界面

<body style="background:url(Html/images/bg.jpg) no-repeat;">
    <form action="/Home/Login" method="post">
        
 <div class="container wrap1" style="height:450px;">
            <h2 class="mg-b20 text-center">单木不林后台登录页面</h2>
            <div class="col-sm-8 col-md-5 center-auto pd-sm-50 pd-xs-20 main_content">
                <p class="text-center font16">用户登录</p>
                <form>
                    <div class="form-group mg-t20">
                        <i class="icon-user icon_font"></i>
                        <input type="text" class="login_input" id="Email1" name="name" placeholder="请输入用户名" />
                    </div>
                    <div class="form-group mg-t20">
                        <i class="icon-lock icon_font"></i>
                        <input type="password" class="login_input" id="Password1" name="password" placeholder="请输入密码" />
                    </div>
                    <div class="checkbox mg-b25">
                        <label>
                            <input type="checkbox" />记住我的登录信息
                        </label>
                    </div>
                    <button type="submit" class="login_btn">登 录</button>
               </form>
        </div><!--row end-->
    </div><!--container end-->
    </form>
   
   
</body>

  界面

 

 

 

百度云:https://pan.baidu.com/s/1zY8Hhwrnc5adkVg_TLirzw   提取码:ytnw

posted @ 2019-10-05 22:40  林小强  阅读(7978)  评论(11编辑  收藏  举报