首页 新闻 会员 周边

网页登录问题

0
[已解决问题] 解决于 2014-07-04 12:26

具体问题描述:类似于淘宝登录过程,先做一个登录页面,通过登录页面验证登录信息,然后登录信息就可以在以后的各个页面中用到此信息。能编写一个案例最好,一个登录界面和另一个页面,还有一个后台代码,大神们求救

東東東東的主页 東東東東 | 初学一级 | 园豆:193
提问于:2014-06-20 22:35
< >
分享
最佳答案
0

 后台代码

 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>
奖励园豆:5
xiaoafei1991 | 菜鸟二级 |园豆:466 | 2014-06-21 21:43
其他回答(1)
0

不是用session的吗?

angelshelter | 园豆:9887 (大侠五级) | 2014-06-21 16:24

怎样 做呢

支持(0) 反对(0) 東東東東 | 园豆:193 (初学一级) | 2014-06-21 19:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册