After login only user should access other pages
I can access index page without login i want that user should login first after that index page could access.
LoginController
public ActionResult Index(login LgnUsr)
{
bool Isvalid = objCon.UserMasters.Any(x => x.MobileNo == LgnUsr.MobileNo && x.EmailVerification == true &&
x.Password == LgnUsr.Password);
var Det = objCon.UserMasters.Where(x => x.MobileNo == LgnUsr.MobileNo &&
x.Password == LgnUsr.Password).ToList<UserMaster>();
//if (Det.Find())
if (Det.Count > 0)
{
if (Det.FirstOrDefault().EmailVerification == false)
{
var GenarateUserVerificationLink = "/Register/UserVerification/" + Det.FirstOrDefault().ActivetionCode;
var link = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, GenarateUserVerificationLink);
// TempData["ErrorMSG"] = "Email Verification in Pending.";
ModelState.AddModelError("", "Email Verification in Pending !");
ViewBag.link = link;
return View();
}
}
if (Isvalid)
{
int timeout = LgnUsr.Rememberme ? 60 : 30; // Timeout in minutes, 60 = 1 hour.
var ticket = new FormsAuthenticationTicket(Det.FirstOrDefault().Email, false, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = System.DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;
Response.Cookies.Add(cookie);
FormsAuthentication.SetAuthCookie(Det.FirstOrDefault().Email, true);
Session["UserName"] = Det.FirstOrDefault().Email;
Session["FName"] = Det.FirstOrDefault().UserFullName;
Session["LName"] = Det.FirstOrDefault().LastName;
Session["UserId"] = Det.FirstOrDefault().UserId;
Session["Code"] = Det.FirstOrDefault().ActivetionCode;
return RedirectToAction("Dashboard", "Home");
}
else
{
ModelState.AddModelError("", "Username or Password is Invalid !");
//TempData["ErrorMSG"] = "Username or Password is Invalid !";
// ModelState.Clear();
}
return View();
}
Web.config
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" timeout="2880" />
</authentication>