simple membership provider in mvc

前端 未结 4 2019
南方客
南方客 2021-01-16 04:06

How to make simple Membership provider from empty web application template in ASP.NET MVC4

I searched a lot on google, bing and many others, but I din\'t get positiv

相关标签:
4条回答
  • 2021-01-16 04:47

    So you want to use simple membership in asp.net MVC4 .
    Follow the steps mentioned in this tutorial : Simple Membership

    This will provide the all the basic information of how to setup simple membership in asp.net mvc4.

    0 讨论(0)
  • 2021-01-16 04:48

    I followed these steps:

    So before starting I am assuming you have setup your database models including a users model which we will use for simple membership. Go ahead and add a "username" column and an "id" column (if you don't already have one) in the model and create the database. Remember already having a database is necessary if you want to use simple membership with your existing user's table. Simple membership will add it's table to your existing database.

    1.Install Webmatrix.webdata and webmatrix.data from Nuget Packet manager.

    2.In your web.config enable simple membership by

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

    in appsettings

    3.Next step is to define profile, role and membership providers in system.web by

    <profile defaultProvider="SimpleProfileProvider">
          <providers>
            <add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="YOUR_CONNECTION_STRING" applicationName="/" />
          </providers>
        </profile>
        <membership defaultProvider="SimpleMembershipProvider">
          <providers>
            <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
          </providers>
        </membership>
        <roleManager enabled="true">
          <providers>
            <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
          </providers>
        </roleManager>
    

    4.Next step is to connect your user table with simple membership. Now the Internet Application Template which is being provided by default uses a filter but we're going to skip it. We're going to directly initialize simple membership in global.asax by

    if (!WebSecurity.Initialized)
                {
                    WebSecurity.InitializeDatabaseConnection("YOUR_DB_CONTEXT", "USER_TABLE", "ID_COLUMN", "USERNAME_COLUMN", true);
                }
    

    And you are done with the setup.

    Now to actually create users, there are two options, Create User and Account and Create Account only. How the create user and account works is that it will you will provide the user's information and it will create a user in your user table and then create a membership account. I personally use the latter, which means I create the users separately, and then create a membership account for that user using

    WebSecurity.CreateAccount("Username","Password");
    
    0 讨论(0)
  • 2021-01-16 04:50

    When you create the new project, select the Internet Template. When you register your first user it will automatically create the table structure in your db

    0 讨论(0)
  • 2021-01-16 04:53

    Just create an Internet template, and copy the code out of it into your empty project.. although, honestly at that point you've essentially got the Internet template anyways, other than the default layout.

    There's a lot of code that goes into supporting the Membership system, so study the Internet template and it will tell you everything you need to know.

    0 讨论(0)
提交回复
热议问题