title: MVC, .NET 5.0 - How to filter Index/Create pages based on database value?
body: <p>I am working on an ASP.NET Core MVC app using EF Core on .NET 5.0. I have configured my application's Startup.CS file to implement Azure AD Authentication per this documentation:</p>
  <p><a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp</a></p>
  <p>And in my <code>Shared/_Layout.cshtml</code> file, I can access/display my user's e-mail address with:</p>
  <pre><code>&lt;span&gt;Signed in as: @User.Identity.Name&lt;/span&gt;
  </code></pre>
  <p>I have a table called &quot;Users&quot; in my SQL Server database that associates an OrganizationID with each user's e-mail address. Possible values are 1, 2, and 3 for Organizations 1, 2, and 3 and then value 99 is an Administrator who should have access to everything from Organizations 1, 2, and 3. For the visual effect the &quot;Users&quot; table/model , it looks like:</p>
  <pre><code>+--------+-----------------+----------------+
  | UserID | EmailAddress    | OrganizationID |
  +--------+-----------------+----------------+
  | 1      | User1@email.com | 1              |
  +--------+-----------------+----------------+
  | 2      | User2@email.com | 2              |
  +--------+-----------------+----------------+
  | 3      | User3@email.com | 3              |
  +--------+-----------------+----------------+
  | 4      | Admin@email.com | 99             |
  +--------+-----------------+----------------+
  </code></pre>
  <p>I have another table/model that has records related to OrganizationID, let's call it &quot;Courses&quot;:</p>
  <pre><code>+----------+------------+----------------+
  | CourseID | CourseName | OrganizationID |
  +----------+------------+----------------+
  | 1        | Math       | 1              |
  +----------+------------+----------------+
  | 2        | Science    | 1              |
  +----------+------------+----------------+
  | 3        | Art        | 2              |
  +----------+------------+----------------+
  | 4        | Literature | 3              |
  +----------+------------+----------------+
  </code></pre>
  <p>When I scaffold the CRUD pages for &quot;Courses&quot; - when a user goes to the Index page, I only want them to see Courses for their own OrganizationID. If they are an Administrator, I want them to see all Courses.</p>
  <p>When users go to the &quot;Courses&quot; Create page, I want the drop-down list for Organization to default to their own OrganizationID (and to be unchangeable) unless they are an Administrator, in which case I want for them to be able to manually select the OrganizationID.</p>
  <p>I think this is handled in the controller for each page/Action? But I don't know how to access the OrganizationID based on their user's e-mail address and I don't know how to modify my controller to do so. After scaffolding CRUD pages, the controller for the &quot;Courses&quot; Index page looks like:</p>
  <pre><code>public async Task&lt;IActionResult&gt; Index()
          {
              var context_variable = _context.FormPage.Include(n =&gt; n.OrganizationNavigation);
              return View(await context_variable.ToListAsync());
          }
  </code></pre>
  <p>Is this where I modify the Index to filter certain records? How about for the Create page?</p>