InitializeSimpleMembershipAttribute.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Data.Entity;
  3. using System.Data.Entity.Infrastructure;
  4. using System.Threading;
  5. using System.Web.Mvc;
  6. using WebMatrix.WebData;
  7. using EMIS.Web.Models;
  8. namespace EMIS.Web.Filters
  9. {
  10. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
  11. public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
  12. {
  13. private static SimpleMembershipInitializer _initializer;
  14. private static object _initializerLock = new object();
  15. private static bool _isInitialized;
  16. public override void OnActionExecuting(ActionExecutingContext filterContext)
  17. {
  18. // Ensure ASP.NET Simple Membership is initialized only once per app start
  19. LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
  20. }
  21. private class SimpleMembershipInitializer
  22. {
  23. public SimpleMembershipInitializer()
  24. {
  25. try
  26. {
  27. WebSecurity.InitializeDatabaseConnection("EMISNewContext", "Sys_User", "UserId", "Name", autoCreateTables: true);
  28. }
  29. catch (Exception ex)
  30. {
  31. 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);
  32. }
  33. }
  34. }
  35. }
  36. }