如何根据用户是否登录来更改“布局”页面?

时间:2015-08-31 15:42:28

标签: c# asp.net-mvc session cookies

我希望用户确定要登录的方式是将他的用户名放在会话存储中,然后找到与数据库中该用户名对应的User对象。

这是LoginSignUp在控制器中的显示方式:

 public ActionResult SignUp()
    {
        return View();
    }

    [HttpPost]
    public ActionResult SignUp(User _user)
    {
        _user.Authorize = CustomRoles.RegisteredUser;
        int lastuserid = entities.Users.Last().UserID;
        _user.UserID = lastuserid + 1;
        if (ModelState.IsValid)
        {
            Roles.AddUserToRole(_user.UserName, CustomRoles.RegisteredUser);
            entities.Users.Add(_user);
            entities.SaveChanges();
            RedirectToAction("Index");
        }
        return View(_user);
    }

    public ActionResult Login()
    {
        LoginViewModel LVM = new LoginViewModel();
        HttpCookie existingCookie = Request.Cookies["UserName"];
        if (existingCookie != null)
        {
            LVM.UserName = existingCookie.Value;
        }
        Session["UserName"] = LVM.UserName;
        return View(LVM);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginViewModel u)
    {
        if (ModelState.IsValid)
        {
            if (u.RememberMe)
            {
                HttpCookie existingCookie = Request.Cookies["UserName"];
                if (existingCookie != null)
                {

                    existingCookie.Value = u.UserName;
                    existingCookie.Expires = DateTime.Now.AddHours(-20);
                }


                HttpCookie newCookie = new HttpCookie("UserName", u.UserName);
                newCookie.Expires = DateTime.Today.AddMonths(12);
                Response.Cookies.Add(newCookie);
            }
            var v = entities.Users.Where(a => a.UserName.Equals(u.UserName) && a.Password.Equals(u.Password)).FirstOrDefault();
            if (v != null)
            {
                return RedirectToAction("Index");
            }

        }
        return View(u);
    }

这是实际的Login页面:

    @{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model Car_Dealership.Models.LoginViewModel
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{

    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)


if (@ViewBag.Message != null)
{
    <div style="border:1px solid red">
        @ViewBag.Message
    </div>
}
    <div>
        <fieldset>
            <legend>Login</legend>
            <div class="editor-label">
                @Html.LabelFor(u => u.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(u => u.UserName)
                @Html.ValidationMessageFor(u => u.UserName)
            </div>
            <div class="editor-label">
                @Html.LabelFor(u => u.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(u => u.Password)
                @Html.ValidationMessageFor(u => u.Password)
            </div>

            <div class="editor-label">
                @Html.LabelFor(u => u.RememberMe)
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(u=>u.RememberMe)
                @Html.ValidationMessageFor(u=>u.RememberMe)
            </div>

            <input type="submit" value="Login" />

            <a href="/Home/SignUp">Sign Up</a>
        </fieldset>
    </div>
}

并更改布局我将其放在_ViewStart

    @{
    if (Session["UserName"]!=null)
    {
        Layout = "~/Views/Shared/UserLayout.cshtml";
    }
    else { Layout = "~/Views/Shared/_Layout.cshtml"; }


}

Session["UserName"]Login获取来自已存在的Cookie的第一个LVM动作方法中的用户名会发生什么情况,然后我认为一切都只是按我认为的那样做要做但我仍然没有得到不同的布局页面。我认为它在LINQ中被搞砸了,我试图将User对象变成v,但我不太确定。

0 个答案:

没有答案