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

  • Home
  • SEARCH
  • 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 6213077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:37:53+00:00 2026-05-24T06:37:53+00:00

I have a project that I’ve built without using the default template, instead I

  • 0

I have a project that I’ve built without using the default template, instead I did everything from beginning.

Now I need to implement checks for admin and I don’t think that

public ActionResult someAction()
{
   if (session exists)
   {
      // do it
   }
   else
   {
      //redirect back or show 403
   }
}

is a good idea on every delete/edit/create actions.

What I want to do instead is build action filter that will check if admin session exists and if there is no session it will redirect to 403 or something like that.

[AdminCheck]   
public ActionResult someAction()
{
   // do it
}

However I don’t know how to do that. I’ve did some research and put it up, but I have no idea how to implement functionality in it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace stevePortfolio.Infrastructure
{
    public class AdminCheck : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            // No idea what to write here...
        }
    }
}
  • 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-05-24T06:37:54+00:00Added an answer on May 24, 2026 at 6:37 am

    You should use the AuthorizeAttribute for this. You can use it out of the box to check if the user is a member of a specific role like this:

    [Authorize(Roles = "IsAdmin")]   
    public ActionResult DoStuff()
    {
       //action body
    }
    

    or you can Subclass it if you need more complexity and place in the required code.

    public class AuthorizeByRightAttribute : AuthorizeAttribute
        {
           protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
            {
                var authorized = base.AuthorizeCore(httpContext);
                if (authorized && controller != null)
                {
                    //Return true or false based on some criteria
                }
    
            }
    

    You can then handle an unauthorized request any way you want. The example below does it by issuing a HTTP status code 403 and a jsonresult for my ajax methods to check, or for normal http requests, redirects to the “Not Authorized” page.

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
            {
                if (!controller.PortalSession.ValidSession)
                {
                    base.HandleUnauthorizedRequest(filterContext);
                }
                else
                {
                    if (filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
                    {
                        //base.HandleUnauthorizedRequest(filterContext);
                        filterContext.RequestContext.HttpContext.Response.StatusCode = 403;
                        var result = new JsonResult();
                        result.Data = new {Success=false};
                        result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                        filterContext.Result = result;
                        return;
                    }
    
                    filterContext.Result = new RedirectToRouteResult(
                        new RouteValueDictionary
                            {
                                {"controller", MVC.Login.Name},
                                {"action", MVC.Login.ActionNames.NotAuthorized},
                                {"group", RequiredRole}
                            });
                }
    
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Java project that I formerly debugged using Sysdeo Launcher. Now I'd like
I have a project that uses SystemConfiguration.Framework. I've been using xcode, where adding the
I have a project that I'm upgrading from 2008 to 2010. My problem is
I have project that creates dlls. These dlls are invoked from python later by
I have a project that has mainly two objects, both inheriting from a base.
I have a project that I am trying to fix from a guy that
is it ok if i have this X project using .net2.0, and that X
I have a project that uses coredata and i am attmepting to delete from
I have project that I'm working on that is going to require a webserver.
I have a project that I'm currently working on but it currently only supports

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.