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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:29:35+00:00 2026-06-15T03:29:35+00:00

Looking for ideas to guard against malicious data changes: userA manipulating (editing or deleting)

  • 0

Looking for ideas to guard against malicious data changes: userA manipulating (editing or deleting) data that belongs to userB. Since we are creating entities on the client, we need to assign them (or at least some of them) to the authenticated user.

For example:

var newItem = ds.createNewItem();
newItem.OwnerId(22); //this is the problem that I see.    
newItem.Name("New Item");
newItem.Description("I just changed your item!");
... //and so on
ds.saveChanges();

Assuming we know the identity of the user calling SaveChanges on our API, how do we validate our entities (new or modified) against this user?

The first thought that comes to mind is to subclass EFContextProvider, override BeforeSaveEntity and examine the entities OwnerId property against the identity of our user. For example:

if (entityInfo.Entity.GetType() == typeof(Item)
    && (entityInfo.EntityState == EntityState.Added 
    || entityInfo.EntityState == EntityState.Modified)
    && ((Item)entityInfo.Entity).OwnerId != _currentUserId) {
    return false
    ... //and so on

If using this approach, does it make sense to establish _currentUserId in the constructor of our new EFContextProvider class?

An ideas or perhaps a better way to approach this problem?

  • 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-15T03:29:37+00:00Added an answer on June 15, 2026 at 3:29 am

    I think you are on the right track. I’ve been noodling this myself and have gone down much the same path.

    Let’s assume you’ve handled authentication and there’s an IPrincipal available. You’ve got yourself a custom IIdentity too (call it AppIdentity) where you can stash the UserId for the authenticated user.

    The Web Api’s base ApiController class makes the ambient IPrincipal available via its User property. We will leverage that in your custom Breeze Web Api controller which might begin like this:

    [Authorize]
    [JsonFormatter, ODataActionFilter]
    public class BreezeApiController : ApiController
    {
        private readonly AppContextProvider _context;
    
        public BreezeApiController() {
            // pass 'User' IPrincipal to the context ctor
            _context = new AppContextProvider(User);
        }
    
        ...
    
        // one of the Query action methods
        [HttpGet]
        public IQueryable<Foo> Foos() {
            return _context.Foos
        }
    
        ...
    

    Your custom EFContextProvider might begin like this:

    public class AppContextProvider : EFContextProvider<AppDbContext>
    {
        public AppContextProvider(IPrincipal user)
        {
            UserId = ((AppIdentity) user.Identity).UserId;
        }
    
        public int UserId { get; private set; }
        ...
    
    

    Now you probably want to prevent UserB’s entities from being seen by UserA. So instead of allowing every Foo to go out the door, your custom EFContextProvider could filter accordingly.

       public DbQuery Foos
       {
           get 
           { 
               // Here the 'Context' is your EF DbContext
               return (DbQuery) Context.Foos
                   .Where(f => f.UserId == UserId); 
           }
       }
    

    Looking back at the controller, we see that its Foos GET action method is oblivious to the filter … as it should be. We want our controllers to be light and move the business logic to the custom EFContextProvider and its helpers.

    Finally, a highly simplified, general purpose BeforeSaveEntity could look like this:

    private bool BeforeSaveEntity(EntityInfo info)
    {
        var entity = info.Entity;
        if (info.EntityState == EntityState.Added)
        {
            entity.UserId = UserId;
            return true;
        }
        return UserId == entity.UserId || throwCannotSaveEntityForThisUser();
    }
    
    ...
    
    private bool throwCannotSaveEntityForThisUser()
    {
        throw new SecurityException("Unauthorized user");
    }
    

    Notice that the custom context provider on the server is responsible for setting the UserId of added entities. We wouldn’t trust the client to do that anyway. And of course it is responsible for verifying the UserId of modified and deleted entities.

    Hope this helps. Remember, this is only a sketch. The real deal would have greater sophistication and be refactored into helpers.

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

Sidebar

Related Questions

I'm looking for ideas/suggestions on a namespace. I have 3 objects that do the
I am looking for ideas but what I want to know is that I
I'm looking for ideas on how to detect objects that are in the HTML
I'm looking for ideas on something simple to write that I can use to
This is a question that involves style more than anything. I'm looking for ideas
I'm looking for ideas for a Neural Networks project that I could complete in
I'm looking for ideas on how to automatically track the job that calls the
Im looking for ideas on how to effectively notify users that their input into
Looking for ideas on how to detect when our Windows application is running in
I'm looking for ideas on how to implement a way to get web based

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.