• asp.net mvc 使用linq登录并且判断登录的想法


    就用默认的框架...

    Index.aspx代码如下

    <div>
        <form action="/home/login" method="post">
            name:<input id="name" name="name" type="text" /><br /><br />
            pass:<input id="pass" name="pass" type="text" /><br /><br />
           
            <input type="submit" value="Save" />
        </form>
    </div>

    在HomeController.cs中键入以下代码

     [HandleError]
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                Session["w"] = null;//session写入空值

              
                return View();
            }

            public ActionResult About()
            {
                ViewData["Title"] = "About Page";
                if (Session["w"] != null) //判断是否登录
                {
                    return View();
                }
                else
                {
                    return View("index");
                }
            }
            public ActionResult login()
            {
                string name = Request.Form["name"].Trim();//取得用户名
                string pass = Request.Form["pass"]Trim();//取得密码

                LinqDataContext db = new LinqDataContext();//数据库上下文
                var a = from b in db.user
                        where b.name == name && b.pass == pass//判断是否存在用户
                        select b;
                if (a.Count() ==1)//如果有一个正确值就写入session并且到about
                {
                    Session["w"] = name;
                    return View("About");
                }
                else
                {
                    return View("Index");//错误就返回到index
                }
            }
        }
    }

  • 相关阅读:
    A1047 Student List for Course [unordered_map]
    .net 事务处理的三种方法
    SQline安装
    LeetCode 21 _ 合并两个有序链表
    LeetCode 191 _ 位1的个数
    LeetCode 268 _ 缺失数字
    LeetCode 190 _ 位运算
    LeetCode 136 _ 位运算
    LeetCode 461 _ 位运算
    LeetCode 125 _ 字符串
  • 原文地址:https://www.cnblogs.com/ZilchWei/p/1311103.html
Copyright © 2020-2023  润新知