In Mvc 3, controller class can be added only in the "Controllers" folder ,adding controller class to other folder is not allowed but in Mvc 4, we can add controller class to other folder.
Step 1: Create a "Data" folder inside "Controllers" folder.
Step 2: Add Controller inside Data folder.
Thursday, 2 January 2014
Wednesday, 1 January 2014
Implement Simple Membership , Creating Role & Register admin account With Simple Membership MVC 4
For Creating Role,Login,Registration first we have to Initialize simple membership.
For implement simple membership follow 2 easy steps.
Step 1: Initialize membership in Global.asax file. Copy this function in Global.asax
This function create Simple Membership tables in given "ConnectionString" and initialize membership.
Step 2: Add settings in web config.
For Creating Role we use System.Web.Security.Roles Namespace
For Register Admin we use WebMatrix.WebData.WebSecurity namespace
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.
Labels:
MVC
,
MVC 4
,
Simple Membership
Subscribe to:
Posts
(
Atom
)