I can't make WebSecurity object work anywhere except what's already been generated in AccountController.cs file. Account controller has [InitializeSimpleMembership]
attribute set at the top. Login functions don't complain about calling WebSecurity.Login(...)
, for example. I added a child action to AccountController:
[ChildActionOnly]
[AllowAnonymous]
public ActionResult NavBar()
{
NavBarViewModel viewModel = new NavBarViewModel();
viewModel.LinkItems = new List<NavBarLinkItem>();
if (Request.IsAuthenticated)
{
SimpleRoleProvider roleProvider = new SimpleRoleProvider();
if (roleProvider.IsUserInRole(User.Identity.Name, "User"))
{
viewModel.LinkItems.Add(new NavBarLinkItem()
{ Title = "Create Project", Action = "Create", Controller = "Project" });
}
}
viewModel.LinkItems.Add(new NavBarLinkItem() { Title="Help", Action="Index", Controller="Help" });
return PartialView("_NavBar", viewModel);
}
Left as is, the code crashes on "if (roleProvider.IsUserInRole(User.Identity.Name, "User"))" line with the subject error message. So I go into InitialzeSimpleMembershipAttribute.cs file and copy/paste this line at the top of my function:
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
... and get an error message that WebSecurity.InitializeDatabaseConnection should only be called once. This makes sense, because there is an attribute at the top of the controller definition that should've called this function already (and it seems it does that just fine). So to be safe, I change above call to:
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId",
"UserName", autoCreateTables: true);
}
... and get back the original error message, that WebSecurity.InitializeDatabaseConnection should be called before blah blah blah. Any insight into this madness would be greatly appreciated