具体问题描述:类似于淘宝登录过程,先做一个登录页面,通过登录页面验证登录信息,然后登录信息就可以在以后的各个页面中用到此信息。能编写一个案例最好,一个登录界面和另一个页面,还有一个后台代码,大神们求救
后台代码
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 7 namespace MvcApplication1.Controllers 8 { 9 public class HomeController : Controller 10 { 11 // 12 // GET: /Home/ 13 14 public ActionResult Index() 15 { 16 if (Session["User"]==null) 17 { 18 Response.Redirect("/Home/Login"); 19 } 20 return View(); 21 } 22 public ActionResult Login() 23 { 24 return View(); 25 } 26 [HttpPost] 27 public ActionResult Login(string txtName, string txtPwd) 28 { 29 var result = txtName == "admin" && txtPwd == "666666" ? "1" : "0";//这里改成从数据查 30 if (result=="1") 31 { 32 Session["User"] = "User";//这里改成查到的用户对象 33 } 34 return Content(result); 35 } 36 } 37 }
登录页面代码
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> <script src="~/Scripts/jquery-1.7.1.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> <script type="text/javascript"> $(function () { }); function afterSubmit(data) { if (data == "1") { window.location.href = '@Url.Action("Index","Home")'; } else { alert('用户或密码不正确!'); } } </script> </head> <body> @using (Ajax.BeginForm("Login", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "afterSubmit" })) { <table style="position: relative; left: 470px; top: 320px; width: 260px;"> <tr> <td align="center"><font style="font-size: 12px; color: #ffffff; font-weight: bold">用户名:</font></td> <td align="left"><input type="text" id="userName" name="txtName" style="width: 150px;" /></td> </tr> <tr> <td align="center"> <font style="font-size: 12px; color: #ffffff; font-weight: bold">密码:</font> </td> <td align="left"> <input type="password" id="userPwd" name="txtpwd" value="" style="width: 150px;" /> </td> </tr> <tr> <td align="right" colspan="2"><input type="submit" value="登 录" style="border:0px; cursor: pointer;width: 75px;height: 24px;" /></td> </tr> </table> } </body> </html>
登录成功跳转后的页面代码
1 @{ 2 Layout = null; 3 } 4 5 <!DOCTYPE html> 6 7 <html> 8 <head> 9 <meta name="viewport" content="width=device-width" /> 10 <title>Index</title> 11 </head> 12 <body> 13 <div> 14 <h2>登录后的页面</h2> 15 </div> 16 </body> 17 </html>
不是用session的吗?
怎样 做呢