Injecting a repository in a custom role provider gives me DataContext accessed after Dispose error
I am currently building an ASP.NET MVC application and I want to take advantage of the authorize attribute with roles without having to use the default provider and all its tables that are automatically generated.
I decided to build my own custom role provider (my class is inheriting from RoleProvider) and implement only one开发者_如何转开发 method:
public override string[] GetRolesForUser(string username)
{
Account account = accountRepository.FindByUserName(username);
string[] role = new string[] { account.Role };
return role;
}
My controller looks like:
[Authorize(Roles = "Administrator")]
public ActionResult Index()
{
return View();
}
I am using Unity to inject the repository into my custom role provider class which works just fine but when the FindByUserName(username) is called the error I am getting is:
Cannot access a disposed object. Object name: 'DataContext accessed after Dispose.'.
For some reason Linq2Sql does not like what is going and I am a bit at a loss here. Any insight would be tremendously helpful. Also, if anyone has managed to do what I am trying to do without using any role provider please let me know. The account table in my database has a column called role (int) and my code has an enum with my roles.
Thanks
精彩评论