I have an MVC 5 project using Asp.Net Identity 2.0. I am also using a generic repository pattern. As part of the database schema I have fields for various tables which store the user id of the inserting/updating/deleting user. Therefore I would like to pass the user object, or user id at least to the generic repository to be used when modifying records.
However, as i was unable to access the identity directly in the repository class I am attempting to pass it on repository instantiation. It looks like this:
using System.Data.Entity;
using System.Threading.Tasks;
using System.Net;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using My.Models;
namespace My.Controllers
{
[Authorize]
public class FooController : MasterController
{
private IGenericRepositoryAsync<Topic> _repository;
public FooController()
{
//Point A
_repository = new GenericRepositoryAsync<Foo>(User.Identity);
}
public async Foo<ActionResult> Index()
{
//POINT B
//_repository = new GenericRepositoryAsync<Foo>(User.Identity);
return View(await _repository.GetAllAsync());
}
}
}
As an authorized user at "Point A" User.Identity is null, at //Point B it is not null but I don't want to have to put the repository initializer in every controller Action.
Your help and feedback is appreciated.