For implement simple membership follow 2 easy steps.
Step 1: Initialize membership in Global.asax file. Copy this function in Global.asax
void initializeMembership() { if (!WebSecurity.Initialized) WebSecurity.InitializeDatabaseConnection("ConnectionString", "UserProfile", "UserId", "UserName", autoCreateTables: true); }And Call this function in Application_Start().
This function create Simple Membership tables in given "ConnectionString" and initialize membership.
Step 2: Add settings in web config.
<profile defaultProvider="SimpleProfileProvider"> <providers> <add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="ConnectionString" applicationName="/" /> </providers> </profile> <membership defaultProvider="SimpleMembershipProvider"> <providers> <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" /> </providers> </membership> <roleManager defaultProvider="SimpleRoleProvider"> <providers> <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" /> </providers> </roleManager>Now your simple membership tables are created and simple membership is Initialized.
Creating Role
For Creating Role we use System.Web.Security.Roles Namespace
var role = System.Web.Security.Roles.Provider; if (!role.RoleExists("Admin")) { role.CreateRole("Admin"); }
Register Admin Account
For Register Admin we use WebMatrix.WebData.WebSecurity namespace
WebSecurity.CreateUserAndAccount("Admin", "admin123");add this user to admin role.
role.AddUsersToRoles(new[] { "Admin" }, new[] { "Admin" });First Parameter usernames and second roles.
No comments :
Post a Comment