且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在简单的MVC成员资格提供

更新时间:2022-12-04 15:26:01

我按照下列步骤操作:

所以,在开始之前我假设你已经设置你的数据库模型,包括我们将使用简单的会员资格的用户模式。来吧,添加一个用户名栏和ID列(如果你不已经有一个)模型和创建数据库。记得已经有一个数据库,如果你想用简单的会员与您现有的用户的表是必要的。简单的会员将增加它的表,以现有的数据库。

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.安装Webmatrix.webdata和包的NuGet经理webmatrix.data。

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

2.In你的web.config实现简单籍由

2.In your web.config enable simple membership by

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

在AppSettings的

in appsettings

3.Next步骤是定义在System.Web程序配置文件,作用和成员由供应商

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一步是将用户表简单的会员连接。现在正在由默认提供的互联网应用程序模板使用过滤器,但我们要跳过它。我们将通过

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);
            }

和您使用安装完成。

现在实际创建的用户,有两种选择,创建用户和帐户,只有创建帐户。如何创建用户帐户和作品是它将你将提供用户信息,它会在用户表中创建一个用户,然后创建一个会员帐户。我个人使用后者,这意味着我分别创建用户,然后创建一个使用该用户的会员帐户

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");