Last active
August 9, 2016 19:49
-
-
Save aaronhoffman/7e30ded25cbd0590497197c4dabde9b4 to your computer and use it in GitHub Desktop.
ASP.NET MVC 4 Membership, Users, Passwords, Roles, Profile, Authentication, Authorization http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- script to create asp.net mvc 4 membership tables | |
CREATE TABLE [dbo].[webpages_Membership]( | |
[UserId] [int] NOT NULL, | |
[CreateDate] [datetime] NULL, | |
[ConfirmationToken] [nvarchar](128) NULL, | |
[IsConfirmed] [bit] NULL, | |
[LastPasswordFailureDate] [datetime] NULL, | |
[PasswordFailuresSinceLastSuccess] [int] NOT NULL, | |
[Password] [nvarchar](128) NOT NULL, | |
[PasswordChangedDate] [datetime] NULL, | |
[PasswordSalt] [nvarchar](128) NOT NULL, | |
[PasswordVerificationToken] [nvarchar](128) NULL, | |
[PasswordVerificationTokenExpirationDate] [datetime] NULL, | |
PRIMARY KEY CLUSTERED | |
( | |
[UserId] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] | |
GO | |
CREATE TABLE [dbo].[webpages_OAuthMembership]( | |
[Provider] [nvarchar](30) NOT NULL, | |
[ProviderUserId] [nvarchar](100) NOT NULL, | |
[UserId] [int] NOT NULL, | |
PRIMARY KEY CLUSTERED | |
( | |
[Provider] ASC, | |
[ProviderUserId] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] | |
GO | |
CREATE TABLE [dbo].[webpages_Roles]( | |
[RoleId] [int] IDENTITY(1,1) NOT NULL, | |
[RoleName] [nvarchar](256) NOT NULL, | |
PRIMARY KEY CLUSTERED | |
( | |
[RoleId] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], | |
UNIQUE NONCLUSTERED | |
( | |
[RoleName] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] | |
GO | |
CREATE TABLE [dbo].[webpages_UsersInRoles]( | |
[UserId] [int] NOT NULL, | |
[RoleId] [int] NOT NULL, | |
PRIMARY KEY CLUSTERED | |
( | |
[UserId] ASC, | |
[RoleId] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] | |
GO | |
ALTER TABLE [dbo].[webpages_Membership] ADD DEFAULT ((0)) FOR [IsConfirmed] | |
GO | |
ALTER TABLE [dbo].[webpages_Membership] ADD DEFAULT ((0)) FOR [PasswordFailuresSinceLastSuccess] | |
GO | |
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_RoleId] FOREIGN KEY([RoleId]) | |
REFERENCES [dbo].[webpages_Roles] ([RoleId]) | |
GO | |
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_RoleId] | |
GO | |
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_UserId] FOREIGN KEY([UserId]) | |
REFERENCES [dbo].[UserProfile] ([UserId]) | |
GO | |
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_UserId] | |
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The using below is needed for "UsersContext" - it will be relative to your project namespace | |
using MvcApplication1.Models; | |
using System; | |
using System.Data.Entity; | |
using System.Data.Entity.Infrastructure; | |
using System.Threading; | |
using System.Web.Http; | |
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
using WebMatrix.WebData; | |
namespace MvcApplication1 | |
{ | |
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | |
// visit http://go.microsoft.com/?LinkId=9394801 | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
WebApiConfig.Register(GlobalConfiguration.Configuration); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
AuthConfig.RegisterAuth(); | |
// Ensure ASP.NET Simple Membership is initialized only once per app start | |
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); | |
} | |
private static SimpleMembershipInitializer _initializer; | |
private static object _initializerLock = new object(); | |
private static bool _isInitialized; | |
private class SimpleMembershipInitializer | |
{ | |
public SimpleMembershipInitializer() | |
{ | |
Database.SetInitializer<UsersContext>(null); | |
try | |
{ | |
using (var context = new UsersContext()) | |
{ | |
if (!context.Database.Exists()) | |
{ | |
// Create the SimpleMembership database without Entity Framework migration schema | |
((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); | |
} | |
} | |
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); | |
} | |
catch (Exception ex) | |
{ | |
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment