Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:26:28+00:00 2026-06-07T14:26:28+00:00

I have an MVC3 web application with a front end and a backend. In

  • 0

I have an MVC3 web application with a front end and a backend. In the backend, each action is protected with an if..then..else redirecting to another action if the user is not logged in (it checks a session). It does this without windows roles.

I was wondering if it was possible to create an attribute that does the check and redirects the user to a login page without having to insert the same code over and over into each action?

EDIT #1

I’ve created a basecontroller which my admin controllers derived from and added the following member following on from what Virus said:

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        try
        {
            if (Session["loggedIn"] == null || (bool)Session["loggedIn"] != true)
            {
                TempData["targetAction"] = ControllerContext.ParentActionViewContext.RouteData.Values["action"] + "/" + ControllerContext.ParentActionViewContext.RouteData.Values["controller"];
                RedirectToRoute("Admin/Login");
            }
        }
        catch (NullReferenceException)
        {
            RedirectToRoute("Admin/Login");
        }
    }

But this code does’nt work. Can someone please tell me what is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:26:31+00:00Added an answer on June 7, 2026 at 2:26 pm

    you can do this easily.

    No need to use attributes

    Just create a common BaseController which is inherited from Controller class,
    and inherit all your controllers from BaseController

    override the OnActionExecuting event in BaseController and put the authorization logic on that.

    the OnActionExecuting event will be executed every time when an action is invoked.

    If you need to redirect to an Action, this code will help

    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Account" } });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making the front end of a asp.net mvc3 web application. A controller
i have created an mvc3 web application that uses forms based authentication. one part
I have written an ASP.NET MVC3 Web application which runs perfectly inside the Visual
This is about asp.net mvc3 web application. We have used Object cache to store
I am working on a web application (asp.net mvc3) I have a Main div.
I have MVC3 web application where we need to populate radio button list with
I have a MVC3 web application. I am using Ninject 2 for dependency injection
I have an mvc3 web application with entity framework 4.1. I've created two roles
I have an ASP.Net MVC3 web application written in C#. I have one ActionResult
I have a asp.net MVC3 web application in which i search for a certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.