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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:28:16+00:00 2026-06-11T15:28:16+00:00

In my Controller before a Model is modified ( updated or deleted ) I

  • 0

In my Controller before a Model is modified (updated or deleted) I am trying to verify that the User performing the action actually owns the object they are trying to modify.

I am currently doing this at the method level and it seems a bit redundant.

[HttpPost]
public ActionResult Edit(Notebook notebook)
{
    if (notebook.UserProfileId != WebSecurity.CurrentUserId) { return HttpNotFound(); }

    if (ModelState.IsValid)
    {
        db.Entry(notebook).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(notebook);
}

Is there a generic way of doing this that could be reusable across various models?

Is it possible to do this with an ActionFilter?

  • 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-11T15:28:17+00:00Added an answer on June 11, 2026 at 3:28 pm

    I can see one problem with what you have – you are relying on user input to perform the security check.

    Consider your code

    if (notebook.UserProfileId != WebSecurity.CurrentUserId)
    

    Notebook has come from model binding. So UserProfileId has come from model binding. And you can quite happily fake that – for example I use Firefox’s TamperData to change the value of the hidden UserProfileId to match my login and away I go.

    What I end up doing (in a service, rather than the controller) is on a post pulling back the record from the database based on the unique id passed (Edit/2 for example would use 2), and then checking User.Identity.Name (well, the passed identity parameter) against the current owner field I have in my returned database record.

    Because I pull back from the database (repository, whatever) an attribute isn’t going to work for this, and I’m not sure you could be generic enough in an attribute’s approach anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def filters = { forUser(controller:'user', action:'*') { before = { user=springSecurityService.getCurrentUser() log.info(came to filter
In MVC, some controller functions require accessing the model before rendering a view, whereas
I have a model which returns to controller arrays of user's information (his posts,
I have a modal view that comes up requiring the user to verify his/her
I need to present a modal view controller before showing a split view controller.
The dilemma I'm using a before_filter in my controller that restricts access to admins.
I need to execute a function before each def in the controller is invoked
Is it possible (and reasonable) to write Controller tests and classes before writing the
I have an image that belongs_to a user and to a binary (physical file
this is most likely a simple answer that i have overlooked.. I am trying

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.