Using MVC 4 SimpleMembership with an existing database-first EF model
Asked Answered
B

2

16

I am trying to use SimpleMembership in my MVC 4 for the first time and I already have an existing database and EF5 model created based on it! I searched a lot but I cant find how I could use it in my case and also to have everything under my own model.

It would be great if somebody can give me an idea how to do this.

Thanks

Beliabelial answered 27/2, 2013 at 12:31 Comment(0)
D
16

Purely as a point of reference, it might be a good idea to create a new Internet Application template of an ASP.NET MVC 4 Web Application project (i.e. via File > New Project).

If you look at the AccountController, as @zms6445 says, it is decorated with an InitializeSimpleMembership attribute. You can find the implementation of this attribute in the InitializeSimpleMembershipAttribute.cs file in the Filters folder within the root directory.

In here, this is the missing part of the puzzle - you need to hook up your existing database so that it is used by the SimpleMembershipProvider. This is the code you need:

private class SimpleMembershipInitializer
{
    public SimpleMembershipInitializer()
    {
        try
        {
            if (!WebSecurity.Initialized)
            {
                WebSecurity.InitializeDatabaseConnection("CONNECTION_STRING_NAME", "USER_TABLE", "USER_ID_FIELD", "USER_NAME_FIELD", autoCreateTables: true);
            }
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Something is wrong", ex);
        }
    }
}

Some things to note:

  1. CONNECTION_STRING_NAME is an entry in your web.config ConnectionStrings - you CANNOT use the model connection string here - the SimpleMembershipProvider does not recognise that format! You need to specify an System.Data.SqlClient connection string, e.g.

    <add name="CONNECTION_STRING_NAME" connectionString="data source=SERVER;initial catalog=DATABASE;user id=USER;password=PASSWORD;" providerName="System.Data.SqlClient" />

  2. USER_TABLE is the table in your database to hold extra user information, such as first name, surname etc. This is linked to the autogenerated tables via the USER_ID_FIELD.

  3. USER_ID_FIELD is usually the primary key of your Users table. It must be of type int.

  4. USER_ID_NAME is a unique name for the user, which could be an Email address.

  5. autoCreateTables is set to true to ensure the tables required for the SimpleMembership to work are created if they don't already exist.

Of course, this code only gets fired if you hit a page via the AccountController, since this has been decorated by the attribute. You could put a breakpoint in there and see it in action.

This should get you started - the Internet Application template is a pretty good template to follow if you get stuck.

Hope this helps.

Deciduous answered 5/3, 2013 at 21:4 Comment(5)
I followed your instruction but I get an TargetInvocationException from LazyInitializer. Any idea about it?Arrear
I found it :) SQL server firewall is cause that error! I hate firewalls :)Arrear
@UmutCanKöseali I think I have same problem, how do u turn sql firewall off?Mozart
@Biplov13, how did you end up getting this to run appropriately?Perutz
I got an azure sql database with resticted ip range basically, I have out of the range. I just add my current IP. It worksArrear
I
3

In your web.config in the appSettings tag, add the line

<add key="enableSimpleMembership" value="true"/>

SimpleMembership is built in so from here you simply need to write

[InitializeSimpleMembership]

above your public class AccountController: Controller

When you want to force a user to log in for a certain page you write in the pages controller

[Authorize]

That tables will be automatically generated in your database. If you want to add more fields to these tables you will need to simply google it.

Here's a link for more information http://weblogs.asp.net/jgalloway/archive/2012/08/29/simplemembership-membership-providers-universal-providers-and-the-new-asp-net-4-5-web-forms-and-asp-net-mvc-4-templates.aspx

Isabel answered 1/3, 2013 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.