Hi SajidHussa,
Using below article i have modified with Layout page.
Check the code.
Controller
public class HomeController : Controller
{
[AllowAnonymous]
public ActionResult Index()
{
return View();
}
[Authorize]
public ActionResult Profile()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Index(User user)
{
UsersEntities usersEntities = new UsersEntities();
int? userId = usersEntities.ValidateUser(user.Username, user.Password).FirstOrDefault();
string message = string.Empty;
switch (userId.Value)
{
case -1:
message = "Username and/or password is incorrect.";
break;
case -2:
message = "Account has not been activated.";
break;
default:
FormsAuthentication.SetAuthCookie(user.Username, user.RememberMe);
return RedirectToAction("Profile");
}
ViewBag.Message = message;
return View(user);
}
[HttpPost]
[Authorize]
public ActionResult Logout()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index");
}
}
View
Layout
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
Welcome
@if (!string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
{
<b>@HttpContext.Current.User.Identity.Name</b>
<br />
<br />
}
<hr />
<div>
@RenderBody()
</div>
<hr />
<footer class="panel-footer">
©@DateTime.Now.Year
</footer>
</body>
</html>
Index
@model User_Login_MVC.User
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
.error {
color: red;
}
</style>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th colspan="3">Login</th>
</tr>
<tr>
<td>Username</td>
<td>@Html.TextBoxFor(m => m.Username)</td>
<td>@Html.ValidationMessageFor(m => m.Username, "", new { @class = "error" })</td>
</tr>
<tr>
<td>Password</td>
<td>@Html.PasswordFor(m => m.Password)</td>
<td>@Html.ValidationMessageFor(m => m.Password, "", new { @class = "error" })</td>
</tr>
<tr>
<td>Remember Me</td>
<td>@Html.CheckBoxFor(m => m.RememberMe)</td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
<td></td>
</tr>
</table>
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@if (@ViewBag.Message != null)
{
<script type="text/javascript">
$(function () {
alert("@ViewBag.Message")
});
</script>
}
</body>
</html>
Profile
@{
ViewBag.Title = "Profile";
Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Logout", "Home", FormMethod.Post))
{
<a href="javascript:;" onclick="document.forms[0].submit();">Logout</a>
}
</div>
</body>
</html>
Screenshot