我登录后,我没有得到退出的选项

本文关键字:退出 选项 登录 | 更新日期: 2023-09-27 18:16:43

我正在做我的第一个MVC web应用程序。我开始与登录,注销和注册。通过创建一个新用户并向数据库中的表添加一条记录,注册工作非常出色。我也可以登录。我遇到的问题是,我登录后,我得到重定向到我的主页,就像它假设,但我没有给出退出的选项。在我的_layout中,我有一个if/else语句,假设检查请求是否经过身份验证,然后给出注销选项,但不是。

我对这个相当陌生,任何帮助都会很感激。我提供的代码,我有到目前为止。

这是我在我的UserController中所拥有的:索引页和登录,注销,注册和isvalid的函数和方法

public class UsersController : Controller
{
    private GroceryListEntities db = new GroceryListEntities();
    [HttpGet]
    public ActionResult LogIn()
    {
        return View();
    }
    [HttpPost]
    public ActionResult LogIn(User u)
    {
        if (IsValid(u.UserName, u.Password))
        {
            FormsAuthentication.SetAuthCookie(u.UserName, false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "Login details are wrong.");
        }
        return View(u);
    }
    [HttpGet]
    public ActionResult Register()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Register(User U)
    {
        if (ModelState.IsValid)
        {
            using (db)
            {
                //you should check duplicate registration here 
                db.Users.Add(U);
                db.SaveChanges();
                ModelState.Clear();
                U = null;
                ViewBag.Message = "Successfully Registration Done";
                //return RedirectToAction("Index", "Home");
            }
        }
        return View(U);
    }
    public ActionResult LogOut()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }
    private bool IsValid(string userName, string password)
    {
        bool IsValid = false;
        using (var db = new sogeti.got.groceries.app.Models.GroceryListEntities())
        {
            var user = db.Users.FirstOrDefault(u => u.UserName == userName);
            if (user != null)
            {
                if (user.Password == password)
                {
                    IsValid = true;
                }
            }
        }
        return IsValid;
    }
    // GET: Users
    public ActionResult Index()
    {
        return View(db.Users.ToList());
    }
登录视图:

@model project.app.Models.User
@{
    ViewBag.Title = "Login";
}
<h2>Login</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "Login Failed, check details");
<div>
    <fieldset>
        <legend>Login Form</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>
        <input type="submit" value="Log In" />
    </fieldset>
</div>
}
@* This below line is for create javascript section *@
@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
}

注册视图:

@model project.Models.User
@{
ViewBag.Title = "Register";
}
<h2>Register</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true, "Registration Failed");
<fieldset>
    <legend>Registration Form</legend>
    @Html.AntiForgeryToken()
    @if (ViewBag.Message != null)
    {
        <div style="border:solid 1px green">
            @ViewBag.Message
        </div>
    }
    <div class="editor-label">
        @Html.LabelFor(model => model.FirstName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.LastName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Email)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.UserName)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.UserName)
        @Html.ValidationMessageFor(model => model.UserName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Password)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Password)
        @Html.ValidationMessageFor(model => model.Password)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.ConfirmPassword)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.ConfirmPassword)
        @Html.ValidationMessageFor(model => model.ConfirmPassword)
    </div>
    <p>
        <input type="submit" value="Register" />
    </p>
</fieldset>
}
<div>@Html.ActionLink("Back to List", "Index")</div>

在_layout中输入:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title</title>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div style="width:auto; background-color:aqua">
    @if (Request.IsAuthenticated)
    {
        <strong>@Html.Encode(User.Identity.Name)</strong>
        @Html.ActionLink("LogOut", "LogOut", "Users")
    }
    else
    {
        @Html.ActionLink("Registration", "Register", "Users")
        <span> | </span>
        @Html.ActionLink("Login", "LogIn", "Users")
    }
  </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - <i>Got Groeries</i> </p>
        </footer>
    </div>
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

我登录后,我没有得到退出的选项

我认为您需要的是在系统的模块部分添加以下几行。webServer标记(这应该在您的Web。配置文件)

<remove name="FormsAuthentication" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />

我有同样的问题,这使得Request.IsAuthenticated始终是false

以下是FormAuthentication的默认值:
<system.web>
  <authentication mode="Forms">
    <forms loginUrl="Login.aspx"
           protection="All"
           timeout="30"
           name=".ASPXAUTH" 
           path="/"
           requireSSL="false"
           slidingExpiration="true"
           defaultUrl="default.aspx"
           cookieless="UseDeviceProfile"
           enableCrossAppRedirects="false" />
  </authentication>
</system.web>

您不需要为表单标记拥有所有这些属性,但是您应该拥有身份验证模式。