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 8645275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:30:31+00:00 2026-06-12T12:30:31+00:00

I was looking for an implementation / example of loading and authorizing a resource

  • 0

I was looking for an implementation / example of loading and authorizing a resource at a controller level. I am looking for the same functionality as load_and_authorize_resource in the cancan gem in ruby on rails.

Has anyone come across one / have an example how to implement something similar using Mvc .Net attributes?

Thanks!

The load_and_authorize_resource behaviour

With rails, controller and model names are linked up by convention. The attribute load_and_authorize_resource takes that to its advantage. When an action is hit that requires an instance of a resource, the load_and_authorize_resource verifies whether the instance of the resource can be accessed. If it can, it will load it up in an instance variable, if it cant, it will return a 404 or any error behaviour you have configured the attribute to produce.

For example, if I have a resource picture, and only user that own a certain picture can edit the picture’s name.

So we would have a Edit action, which obviously would have a pictureId of the picture you want to edit. load_and_authorize_resource would verify whether the current context/user has access to the resource.

Here is a small video introduction of the module.

  • 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-12T12:30:32+00:00Added an answer on June 12, 2026 at 12:30 pm

    I am not aware of the existence of such plugin for ASP.NET MVC. To mimic it’s functionality you could write a custom Authorize attribute though:

    public class LoadAndAuthorizeResourceAttribute : AuthorizeAttribute
    {
        private class ModelDescriptor
        {
            public string Name { get; set; }
            public Type ModelType { get; set; }
        }
    
        private const string ModelTypeKey = "__ModelTypeKey__";
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            var parameters = filterContext.ActionDescriptor.GetParameters();
            if (parameters.Length > 0)
            {
                // store the type of the action parameter so that we could access it later
                // in the AuthorizeCore method
                filterContext.HttpContext.Items[ModelTypeKey] = new ModelDescriptor
                {
                    Name = parameters[0].ParameterName,
                    ModelType = parameters[0].ParameterType,
                };
            }
            base.OnAuthorization(filterContext);
        }
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            var authorized = base.AuthorizeCore(httpContext);
            if (!authorized)
            {
                // the user is not authenticated or authorized => no need to continue
                return false;
            }
    
            // get the currently authenticated username
            string username = httpContext.User.Identity.Name;
    
            // get the id of the resource that he is trying to manipulate
            // the id should be sent either as part of the query string or the routes
            string id = httpContext.Request.RequestContext.RouteData.Values["id"] as string;
    
            // get the action param type
            var modelDescriptor = httpContext.Items[ModelTypeKey] as ModelDescriptor;
    
            if (modelDescriptor == null)
            {
                throw new InvalidOperationException("The controller action that was decorated with this attribute must take a model as argument");
            }
    
            // now load the corresponding entity from your database given the 
            // username, id and type
            object model = LoadModel(id, username, modelDescriptor.ModelType);
    
            if (model == null)
            {
                // the model that satisfies the given criteria was not found in the database
                return false;
            }
    
            httpContext.Request.RequestContext.RouteData.Values[modelDescriptor.Name] = model;
    
            return true;
        }
    
        private object LoadModel(string id, string username, Type modelType)
        {
            // TODO: depending on how you are querying your database
            // you should load the corresponding model here or return null
            // if not found
            throw new NotImplementedException();
        }
    }
    

    and now you could have a controller action that is decorated with this attribute:

    [LoadAndAuthorizeResource]
    public ActionResult Edit(Picture model)
    {
        ... if we get that far the user is authorized to modify this model
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for a Repository pattern implementation example/resource that follows domain driven design
I'm looking at an example implementation of a linkedlist consisting of nodes. The set
I'm looking for an example implementation of the template-method pattern in Java. Suppose, for
I'm looking an e-book or some example/samples/tutorials for reach GUI implementation with 3d animation
I am looking for a C# example implementation of a class derived from Microsoft's
I am looking for a good example implementation of a JavaScript/HTML AutoSuggest input box
I am looking at a jqGrid implementation example for a ASP.NET MVC project. But
I'm looking for implementation of IP Traceback using packet marking algorithm in C#? I
I'm looking for implementation of log() and exp() functions provided in C library <math.h>
I have a trait and an implementation looking like: trait Foo[A] { def bar[B

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.