0

I have read a number of links to try and help me solve this problem. I am following my first MVC tutorial through on Pluralsight and I am moving on to using authentication. It uses the MVC4 Internet Application Template.

In my Seed method I have:

protected override void Seed(DepartmentDb context)
{
  if (!Roles.RoleExists("Admin"))
    Roles.CreateRole("Admin");
  if (Membership.GetUser("Luke") == null)
  {
    Membership.CreateUser("Luke", "password");
    Roles.AddUserToRole("Luke", "Admin");
  }
}

The user and role add fine in to SQL and this all seems hunky dory, and is also the same as the tutorial. It then instructs to log in with my credentials. So I try to log in and I get the exception stated in the title:

Membership.Provider must be instance of ExtendedMembershipProvider

I have tried re-installing the packages required and also tried installing it as per a SO post and also adding the SimpleMembership into appsetings which doesn't work.

I have also read this MSDN link as per a comment, which I have followed instructions but still can't solve it.

What else do I need to do to get this working? It is driving me insane.

Thanks,

Luke.

Community
  • 1
  • 1
LukeHennerley
  • 6,344
  • 1
  • 32
  • 50
  • possible duplicate of [MVC4 ExtendedMembershipProvider and entityframework](http://stackoverflow.com/questions/12181186/mvc4-extendedmembershipprovider-and-entityframework) – Dave Swersky Mar 22 '13 at 15:13
  • @DaveSwersky I have looked at that too, couldn't find anything concrete I will have another read though :) – LukeHennerley Mar 22 '13 at 15:14
  • The link in the first answer appears to have some detail. – Dave Swersky Mar 22 '13 at 15:15
  • @DaveSwersky Yeah I went through the article, it says `If you want to use the new AccountController, you'll either need to use the SimpleMembershipProvider or another valid ExtendedMembershipProvider. This is pretty straightforward.` It's not for me! I don't know which bit I need to do from here, it's not clear to me – LukeHennerley Mar 22 '13 at 15:17
  • @DaveSwersky - that link is not a solution to the problem, his problem is different from that one. That one involves using universal membership provider instead of simple membership provider, that is not the case here. – Erik Funkenbusch Mar 22 '13 at 15:20
  • @Luke It looks like you have to have a custom membership provider class based on ExtendedMembershipProvider. Here's another potential resource https://github.com/mazhekin/MVC4CustomMembershipSolution – Dave Swersky Mar 22 '13 at 15:22

1 Answers1

1

The problem here is that you are calling Membership.CreateUser() prior to the SimpleMembershipProvider being initialized. This is initialized via an attribute on the AccountController (which is typically loaded when the user logs in, because the login functions are part of the AccountController.

Unfortunately, your seed function runs before the AccountController is accessed, and thus, the provider is not yet initialized.

Look at the InitializeSimpleMembershipAttribute.cs class, and figure out a way to call this before your seed function runs.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Sorry I musn't of made my OP very clear. The seed section works fine, it's when I log on through the `AccountController` that I get a problem. – LukeHennerley Mar 22 '13 at 15:20
  • @LukeHennerley - Did you add a membership definition in your web.config? If so, then you're overriding the simple membership provider and when you go to logon it is trying to initialize the provider. – Erik Funkenbusch Mar 22 '13 at 15:22
  • I didn't add it myself, I used http://nuget.org/packages/Microsoft.AspNet.Providers which adds `DefaultRoleProvider` and `DefaultMembershipProvider` as per my tutorial into the web.config. Would posting this area of the code make it easier for you to answer? :) – LukeHennerley Mar 22 '13 at 15:25
  • @LukeHennerley - Ok, so you are adding the universal providers then. All you need to do is remove the `[InitializeSimpleMembership]` attribute from the AccountController class, and you may have to remove any code the depends on it. – Erik Funkenbusch Mar 22 '13 at 15:28
  • Yeah I thought that myself, I think that I am going to have to create a custom `AccountController` or something like that? – LukeHennerley Mar 22 '13 at 15:36
  • @LukeHennerley - I would be very surprised that the PluralSight training did not cover this. I'm guessing you must have missed it. – Erik Funkenbusch Mar 22 '13 at 15:39
  • @LukeHennerley - Which tutorial are you following? – Erik Funkenbusch Mar 22 '13 at 15:40
  • I have gone over the material numerous times and the tutorial doesn't do anything different to me in this area, which is the frustrating part :P – LukeHennerley Mar 22 '13 at 15:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26737/discussion-between-lukehennerley-and-mystere-man) – LukeHennerley Mar 22 '13 at 15:40