我的文件夹Shared中有一个Login.aspx文件。当我单击我的logut按钮时,它会转到SessionController-> LogOut操作。我有问题。
我的FormsAuthentication
未登出。
重定向到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 Conttroller的调用:
<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>
答案 0 :(得分:0)
正如异常所说,MVC视图必须继承特定的MVC基础结构类才能被识别为MVC视图。
对于ASPX视图引擎,您需要以明确的方式指定它,例如
<%@ Page Language="C#"
Inherits="System.Web.Mvc.ViewPage<YourModelClassName>" %>
或
Inherits="System.Web.Mvc.ViewPage"
如果您的观点不期望任何模型。
答案 1 :(得分:0)
希望这有帮助
return Redirect("url");
或
return RedirectToRoute("routename"); // Specify route in the Route Config file.