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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:38:18+00:00 2026-06-07T11:38:18+00:00

Background: I’ve been working on a small proof-of-concept web application in ASP.NET 4.0 with

  • 0

Background:
I’ve been working on a small proof-of-concept web application in ASP.NET 4.0 with C# and I’ve been using Linq To SQL because it’s quick and easy. I’ve now got to restrict one user from seeing another user’s data in the same database. So I’ve added an ‘account’ column to most of my tables and I want to check that the current user is a member of the same account as the data they are viewing/editing before allowing them access.

One possible solution I’m not really happy with:
I could simply but painfully go through all of my Linq To SQL queries and add an if statement to do the check like this:

MyDataContext DB = new MyDataContext();
//get the current user's information from the DB
USER myUser = DB.USERs.Where(j => j.USR_EMAIL == User.Identity.Name).FirstOrDefault();
//get the object to edit
MYOBJECT myObject= DB.MYOBJECTs.Where(j => j.ID == IdFromQueryString).FirstOrDefault();
//compare the user's account with the object's account they are now trying to view
if(myUser.AccountID == myObject.AccountID)
{
    //display the object
}
else
{
    //display error message
}

This would be a lot of work to do throughout the entire application and not to mention the repetition and opportunity for mistakes. It will also be painful to maintain and if I forget to add this for a query I will leave a security hole open.

The Question:
Is there a way to do this once for each table and it automatically checks every time I do a query with Linq To SQL? OR Is there a better way to go about this?

  • 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-07T11:38:20+00:00Added an answer on June 7, 2026 at 11:38 am

    You should had used the Repository pattern and not access directly Contest.Users, but always access Repository.Users. Then you could had change the repository to expose Users as from u in InternalContext.Users where u.AccountId = currentAccountId select u thus enforcing the scope. Coincidentally this technique is called a scope in Rails…

    The next best thing is to use DataLoadOptions to associate a lambda filter with your entities, see How to: Filter at the DataContext Level (LINQ to SQL). You would need to add this to every context you create. Again, refactoring the code to obtain the data context from a method (or better, implement a repository pattern…) would help a lot.

    Finally you could go on the back end and implement this via updateable views and context_info() but I would strongly discourage this.

    PS. I hope you made the account_id the leftmost key in every clustered index on the tables, and made the entity id primary key non-clustred, did you? Your performance will tank otherwise.

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

Sidebar

Related Questions

Background: I'm testing a function within an ASP.NET 4.0 (Web Forms not MVC) and
BACKGROUND I'm using VS 2010 on a machine where I installed .Net 4.5 which
Background Developing a simple web application (Eclipse + JBoss + Apache Tomcat) to generate
Background: I am developing a web-based teaching program (using Drupal 7, if that matters)
Background We have a Windows .NET application used by our field employees who travel
Background I have been using the JQuery UI plugin for creating a dialog that
Background: We're building an application that allows our customers to supply data in a
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background - I am using paramiko to put files on a bunch of remote
Background I am working with a monad built of a stack of transformers one

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.