nitializeDatabaseConnection&QUOT

nitializeDatabaseConnection&QUOT

本文介绍了必须调用" WebSecurity.InitializeDatabaseConnection"方法调用&QUOT的任何其他方法之前,WebSecurity"类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我重新启动与Visual Studio我得到这个该死的错误调试:

Everytime I restart Debugging with Visual Studio I get this freaking error:

您必须调用WebSecurity.InitializeDatabaseConnection的方法
  在致电前的WebSecurity类的任何其它方法。这个
  呼叫应被放置在一个_AppStart.cshtml文件中的根的
  站点。

但是,这是每次,当我部署的应用程序督促。我在这里得到这个错误,然后,随机

But that is everytime, and when i deploy the app to prod. I get this error here and then, randomly.

我把适当的标记:

[Authorize(Roles = "admin")]
[InitializeSimpleMembership]
public class IndexController : Controller

到控制器,这里是过滤事情是这样的。它只是不想要的工作。

to controller and here is the filter the way it is. It just doesnt wanna work.

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Threading;
using System.Web.Mvc;
using MeetingTaskManagement.Models;
using WebMatrix.WebData;

namespace MeetingTaskManagement.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed 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. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        }
    }
}

有人可以帮我整理了这一点?

can someone help me sort this out?

推荐答案

从你的控制器中取出SimpleMemberShip属性和垃圾了。以下添加到您的Global.asax。

Remove the SimpleMemberShip attribute from your controllers and trash it. Add the following to your global.asax.

    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();

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

    public class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            using (var context = new UsersContext())
                context.UserProfiles.Find(1);

            if (!WebSecurity.Initialized)
                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        }
    }

这篇关于必须调用&QUOT; WebSecurity.InitializeDatabaseConnection&QUOT;方法调用&QUOT的任何其他方法之前,WebSecurity&QUOT;类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 00:07