I have a Login.aspx file in my folder Shared. When I click my logut button, it takes me to SessionController->LogOut action. I have to problems.
My
FormsAuthenticationis not beign signed out.Gets an error when redirecting to Login.aspx:
System.InvalidOperationException: The view at '~/Views/Shared/Login.aspx' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.
I got this code:
My Controller:
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");
}
}
My calling from HTML to de Conttroller:
<ul class="dropdown-menu">
<li><a id="logout" href="~/Session/LogOut">Log out</a></li>
</ul>
And 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>
As the exception says, MVC views have to inherit specific MVC infrastructure classes in order to be recognized as MVC views.
In case of the ASPX view engine you need to specify this in an explicit way, for example
or
if your view doesn't expect any model.