码迷,mamicode.com
首页 > Web开发 > 详细

[MVC] WebSecurity在VS2013中不识别

时间:2015-05-05 18:22:23      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

使用VS2013创建了MVC4项目以后,在filters中添加了类InitializeSimpleMembershipAttribute ,不能识别UserContext和WebSecurity,发现缺少WebMatrix.WebData的引用,在Nuget控制台中中运行:install-package Microsoft.AspNet.WebPages.Data即可添加,这样解决了WebSecurity的问题。对于UserContext,需要在model中添加一个AccountModel的类。

public class InitializeSimpleMembershipAttribute : ActionFilterAttribute
    {
        private static SimpleMembershipInitializer _initializer;
        private static object _initializerLock = new object();
        private static bool _isInitialized;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Ensure ASP.NET Simple Membership is initialized only once per app start

            LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
        }

        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.", ex);
                }
            }
        }
    }

  

 

[MVC] WebSecurity在VS2013中不识别

标签:

原文地址:http://www.cnblogs.com/csts/p/MVC.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!