0

我的 Shared 文件夹中有一个 Login.aspx 文件。当我单击注销按钮时,它会将我带到 SessionController->LogOut 操作。我有问题。

  1. FormsAuthentication没有退出。

  2. 重定向到 Login.aspx 时出错:

System.InvalidOperationException:“~/Views/Shared/Login.aspx”处的视图必须派生自 ViewPage、ViewPage、ViewUserControl 或 ViewUserControl。

我得到了这个代码:

我的控制器:

public class SessionController : Controller
{
    public ActionResult LogOut()
    {
        if (Request.Cookies["Usuario"] != null)
        {
            FormsAuthentication.SignOut();
            var c = new HttpCookie("Usuario");
            c.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(c);
        }
        return View("Login");
    }
}

我从 HTML 到 de Controller 的调用:

<ul class="dropdown-menu">
    <li><a id="logout" href="~/Session/LogOut">Log out</a></li>
</ul>

和登录.aspx:

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Security " %>
<%@ Import Namespace="System.IO" %>

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Forms Authentication</title>
    <link href="https://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://getbootstrap.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
    <link href="https://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
    <script src="https://getbootstrap.com/assets/js/ie-emulation-modes-warning.js"></script>

    <script runat="server">
        private void Logon_Click(Object sender, EventArgs e)
        {
            string cmd = "User='" + User.Value + "'";
            DataSet ds = new DataSet();
            FileStream fs = new FileStream(Server.MapPath("Users.xml"), FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(fs);
            ds.ReadXml(reader);
            fs.Close();
            DataTable users = ds.Tables[0];
            DataRow[] matches = users.Select(cmd);
            if (matches != null && matches.Length > 0)
            {
                DataRow row = matches[0];
                string hashedpwd = FormsAuthentication.HashPasswordForStoringInConfigFile(Password.Value, "SHA1");
                string pass = (String)row["Password"];
                if (0 != String.Compare(pass, hashedpwd, false))
                    Msg.Text = "<div class='alert alert-danger'>Usuario y contraseña no coinciden.</div>";
                else
                {
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
                        User.Value,
                        DateTime.Now,
                        DateTime.Now.AddDays(5),
                        Persist.Checked,
                        "abc",
                        FormsAuthentication.FormsCookiePath);
                    string encTicket = FormsAuthentication.Encrypt(ticket);
                    Response.Cookies.Add(new HttpCookie("Usuario", encTicket));
                    Response.Redirect("~/Factura");


                }
                /*FormsAuthentication.SetAuthCookie(User.Value, Persist.Checked);
                Response.Redirect("~/Factura");*/
            }
            else
            {
                Msg.Text = "<div class='alert alert-danger'>Usuario y contraseña no coinciden.</div>";
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <form runat="server" class="form-signin">
            <h2 class="form-signin-heading">Login</h2>
            <label for="User" class="sr-only">Email address</label>
            <input id="User" class="form-control" type="text" placeholder="Nombre de usuario" title="Solo numeros y letras" pattern="^[A-Za-z]*$" required autofocus runat="server" />
            <label for="Password" class="sr-only">Contraseña</label>
            <input id="Password" class="form-control" type="password" placeholder="Contraseña" required runat="server" />
            <asp:Literal ID="Msg" runat="server" />
            <div class="checkbox">
                <label>
                    <asp:CheckBox ID="Persist" runat="server"
                        AutoPostBack="true" />
                    Recordar contraseña
                </label>
            </div>
            <input type="submit" class="btn btn-lg btn-primary btn-block" onserverclick="Logon_Click" value="Login"
                runat="server" />
        </form>
    </div>
    <script src="https://getbootstrap.com/assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
4

2 回答 2

0

希望这可以帮助

return Redirect("url");

或者

return RedirectToRoute("routename"); // Specify route in the Route Config file.
于 2016-07-12T21:07:26.690 回答
0

正如例外所说,MVC 视图必须继承特定的 MVC 基础结构类才能被识别为 MVC 视图。

对于 ASPX 视图引擎,您需要以显式方式指定它,例如

<%@ Page Language="C#" 
    Inherits="System.Web.Mvc.ViewPage<YourModelClassName>" %>

或者

    Inherits="System.Web.Mvc.ViewPage"

如果您的视图不期望任何模型。

于 2016-07-12T19:16:24.780 回答