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、视图界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<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>

  界面

 

posted @ 2020-12-30 08:37  nkandkn  阅读(2051)  评论(0编辑  收藏  举报