Allow access to other pages after logged in in ASP.net MVC

Last Reply 2 months ago By dharmendr

Posted 2 months ago

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>

 

Posted 2 months ago

Posted 2 months ago

Getting Error

HTTP Error 500.19 - Internal Server Error

The requested page cannot be accessed because the related configuration data for the page is invalid.


Posted 2 months ago

I have another controller named ApplicantController in that I have index page so that page I want to access agter login only or it should redirect to login page.

ApplicantController 

 

public ActionResult Index()
{
var data = conn.applicants.ToList();
ViewBag.applicantdetails = data;
DateTime cretedat = DateTime.Now;
using (cams20Entities db = new cams20Entities())
{
//.OrderByDescending(x => x.id).ThenByDescending(a => a.id


List <applicant> applicants = db.applicants.ToList();
List<dept> depts = db.depts.ToList();
List<app> apps = db.apps.ToList();
DateTime date = DateTime.Today;
var Recordsall = (from e in applicants
join d in depts on e.DEPT_NO equals d.DEPT_NO into table1

from d in table1.ToList()

join a in apps on e.APP_CODE equals a.APP_CODE into table2

from a in table2.ToList()

select new applicant
{
DEPT_NO = e.DEPT_NO,
DEPT_NAME = d.DEPT_NAME,
ack_no = e.ack_no,
name = e.name,
id = e.id,
APP_TYPE = a.APP_TYPE,
appdate = e.appdate,
remarks = e.remarks,
street = e.street,
village = e.village,
town = e.town,
pincode = e.pincode,
created_at = e.created_at

});
//return View(Recordsall);
var repListTbl = Recordsall.OrderByDescending(x => x.id).ToList();
return View(repListTbl);
}
}

 


Posted 2 months ago

Hello @dharmendr

Issue has been resolved thank you so much


Posted 2 months ago

As you have solved please share the solution and mark as answer.