0

In webform after log in into an account, we use the following code in the Page Load function of each .aspx file to check whether the page is accessible or not:

if (Session["User"]!=null)
    {
       Response.Redirect("LoggedInPage.aspx");

    }

What should I do for performing the same thing in MVC5 ??

Sibnz
  • 63
  • 2
  • 11

2 Answers2

0

after login should go to other action

public ActionResult Login() {
    return RedirectToAction("Index");
}

public ActionResult Index()
{
  return View();
}
Issa Saman
  • 100
  • 9
  • I didn't ask for that. After logging into an account a user should not access the Register View . I am trying to check his access , and using the code in Register View @Issa Saman – Sibnz Dec 20 '17 at 09:11
  • I have 2 Controllers Home and Log In. In Home Controller there is an action named Register . Here one can register his account before logging in into the account and when one have logged into his account, he will acess the LogIn/LoggedInPage View. And then if he try to access the url Home/Register , the Register View will redirect him to LogIn/LoggedInPage. Actually I am wanting this. @Issa Saman – Sibnz Dec 20 '17 at 10:10
0

Now i think i got it you want to prevent user to enter to registration page if he logged in .

Look at the AuthorizeAttribute.

1- ASP.Net MVC: Can the AuthorizeAttribute be overriden?

2- custom authorize

public class CustomAuthorize : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {

        var isAuthorized = base.AuthorizeCore(httpContext);
        if (!isAuthorized)
        {
            return false;
        }

        if (httpContext.User.Identity.IsAuthenticated && Request.Url.ToString().Contains("Register"))
        {
            return false;
        }

    }

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
        else
        {
            filterContext.Result = new RedirectToRouteResult(new
            RouteValueDictionary(new { controller = "Error", action = "AccessDenied" }));
        }
    }
}
Issa Saman
  • 100
  • 9