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 からコントローラーへの私の呼び出し:

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

そして Login.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 に答える