1

我有一个使用 ASP.NET MVC4 的项目,我必须登录并保留当前用户信息,如 id、他的姓名等,以便在其他页面中使用它们

    [HttpPost]
    public ActionResult Auth(string login, string password)
    {
        // Customer c = null;
        Customer cus = (from e in db.Customers where e.Login == login && e.Password == password select e).SingleOrDefault();
        if (cus != null)
        {


            //String s = User.Identity.Name;
            return RedirectToAction("Index");
        }
        else return RedirectToAction("Error");
    }

我想我只需要会话代码,所以请任何人都可以帮助我??

4

2 回答 2

0

我想你可以做这样的事情:

    Customer cus = (from e in db.Customers where e.Login == login && e.Password == password select e).SingleOrDefault();
    if (cus != null)
    {
        // the boolean is for creating a persistent cookie
        // also after this point User.Identity.Name should be == login
        FormsAuthentication.SetAuthCookie(login, true);

        // store more info in session and access it in other places
        Session["Customer"] = cus;

        //String s = User.Identity.Name;
        return RedirectToAction("Index");
    }

也就是说,在Session我上次检查 ASP.NET 会话劫持仍然存在时存储敏感用户信息并不是一个好主意。只存储您需要的东西。希望这可以帮助。

于 2013-05-11T04:28:57.963 回答
0

在 MVC4 中,您可以使用 knockoutjs 和轻风,您可以使用上下文的形式将值从一个视图传递到另一个视图,并可以在视图激活时检索该值。

请查看此链接以获取微风

于 2013-05-11T03:36:26.530 回答