Custom Entity Framework WebMatrix, Membership Provider

EFMembership can Manage Roles and User Accounts over your Database and Website

Welcome

EFMembership gives you Membership Provider for System.Security.Membership or WebMatrix. With supporting OAuthMembership for external login like (Facebook, Twitter, Yahoo, Google, ...).

EFMembership - WebMatrix , Membership Provider

You can use your custom model class or Builtin exist models like my Default models or Microsoft membership style

Install

Use nuget packge manager to add EFMembership and dependency references to your project

For WebMatrix:

PM> Install-Package EFMembership-WebMatrix

For Default Membership Provider:

PM> Install-Package EFMembership

Quick Start

    public class MyMembership :
        OmidID.Web.Security.EFMembershipProvider<
            OmidID.Web.Security.Default.DefaultUser,
            OmidID.Web.Security.Default.DefaultOAuthMembership,
            int> {
    }

    public class MyRole :
        OmidID.Web.Security.EFRoleProvider<
            OmidID.Web.Security.Default.DefaultRole,
            OmidID.Web.Security.Default.DefaultUserRole,
            int> {
    }

Username, UserID, Password, PasswordFormat, PasswordSalt in User Table are required and
UserID, ProviderName, ProviderToken in OAuthMembership Table are required.

using System;
using System.ComponentModel.DataAnnotations;
using OmidID.Web.Security.Mapper;

namespace MyApp {
    [Table("Users")]
    public class MyUser { 

        //If you want to use different name for your model
        [UserColumn(UserColumnType.UserID)]
        [Key]
        public long ID { get; set; }

        [Required]
        [MaxLength(300)]
        public string Username { get; set; }

        [Required]
        [MaxLength(100)]
        public string Password { get; set; }

        [Required]
        public int PasswordFormat { get; set; }

        [MaxLength(100)]
        public string PasswordSalt { get; set; }

        [UserColumn(UserColumnType.Email)]
        [MaxLength(100)]
        public string EmailAddress { get; set; }

        public bool IsApproved { get; set; }

        [UserColumn(UserColumnType.Email)]
        public DateTime CreateOn { get; set; }

    }
}


If you are using WebMatrix:

using System;
using System.ComponentModel.DataAnnotations;
using OmidID.Web.Security.Mapper;

namespace MyApp {
    [Table("OAuthMemberships")]
    public class MyOAuthMembership { 

        [Key]
        public long UserID { get; set; }

        [Required]
        [MaxLength(400)]
        [OAuthMembership(OAuthMembershipColumnType.ProviderName)]
        public string Name { get; set; }

        [Required]
        [MaxLength(400)]
        [OAuthMembership(OAuthMembershipColumnType.ProviderToken)]
        public string Token { get; set; }

    }
}


If you don't want to have Role in your website, you can skip this step
OK. Now we have to create Role and UserRole (to make n to n relation between User and Role )

using System;
using System.ComponentModel.DataAnnotations;
using OmidID.Web.Security.Mapper;

namespace MyApp {   
    [Table("Roles")]
    public class MyRole {

        [Key]
        [RoleColumn(RoleColumnType.RoleID)]
        public int RoleID { get; set; }

        [Required]
        [RoleColumn(RoleColumnType.RoleName)]
        public string RoleName { get; set; }

        [Required]
        [RoleColumn(RoleColumnType.CreateOn)]
        public DateTime CreateOn { get; set; }

    }

}


using System;
using System.ComponentModel.DataAnnotations;
using OmidID.Web.Security.Mapper;

namespace MyApp {   
    [Table("UserRoles")]
    public class MyUserRole {

        [Key]
        [Column(Order = 1)]
        [UserRoleColumn(UserRoleColumnType.RoleID)]
        public int RoleID { get; set; }

        [ForeignKey("RoleID")]
        public MyRole Role { get; set; }


        [Key]
        [Column(Order = 2)]
        [UserRoleColumn(UserRoleColumnType.UserID)]
        public long UserID { get; set; }

        [ForeignKey("UserID")]
        public MyUser User { get; set; }

    }
}


Now you can make your custom Membership:

namespace MyApp {
    public class MyMembership :
        OmidID.Web.Security.EFMembershipProvider<MyUser, MyOAuthMembership, int> 
    {
    }

    public class MyRole :
        OmidID.Web.Security.EFRoleProvider<MyRole, MyUserRole, int> 
    {
    }
}


Congratulation! Now you have your Custom Membership Provider

Add Custom membership to your web.config or app.config file

just open your config file and merge or add those section below to your config file:

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-WebMatrix_Sample1-20140412113758;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-WebMatrix_Sample1-20140412113758.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

  <system.web>
    <membership defaultProvider="MyCustomMembershipProvider">
      <providers>
        <clear />
        <add name="MyCustomMembershipProvider"
             type="MyApp.MyMembership, MyApp"
             tablePrefix="membership_"
             connectionStringName="DefaultConnection"
             enablePasswordRetrieval="false"
             enablePasswordReset="true"
             requiresQuestionAndAnswer="false"
             requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5"
             minRequiredPasswordLength="6"
             minRequiredNonalphanumericCharacters="0"
             requiresEmail="false"
             passwordAttemptWindow="10"
             applicationName="/"></add>
      </providers>
    </membership>


    <roleManager enabled="true" defaultProvider="MyCustomRoleProvider">
      <providers>
        <clear />
        <add name="MyCustomRoleProvider"
             membershipProvider="MyCustomMembershipProvider"
             type="MyApp.MyRole, MyApp"
             tablePrefix="membership_"
             connectionStringName="DefaultConnection"
             applicationName="/" />
      </providers>
    </roleManager>

  </system.web>
</configuration>

Initalize Database Automaticly

If you want to generate your database automaticly you can run the code below in your Application Start:

var membership = System.Web.Security.Membership.Provider;
var role = System.Web.Security.Roles.Provider;
var installer = new OmidID.Web.Security.Installer();

installer.MembershipProvider = membership;
installer.RoleProvider = role;

//Install database:
bool Created = installer.CreateIfNotExist();

Test and Example

You can download full source code from Github and run my Samples to see speed of Register and Authorize users