1

我想访问SimpleMembershipProvider诸如ValidateUser方法之类的全部功能。

因此,根据其文档,我不应该调用WebSecurity.InitializeDatabaseConnection()初始化,而是启用标准成员资格和角色提供者。

我的问题是:如何初始化 SimpleMembershipProvider 类

To finally:可以访问全部功能SimpleMembershipProvider

或者如果有更好的解决方案,谢谢

4

2 回答 2

3

如何初始化 SimpleMembershipProvider 类

如果您查看默认的 ASP.NET MVC 4 Internet 应用程序模板,则 AccountController 使用该[InitializeSimpleMembership]属性进行修饰。这就是它在此示例中的初始化方式。这意味着您一旦通过帐户控制器就可以使用它,而不是之前。如果您想在身份验证之前使用您的会员提供商,您可以在您的Application_Start方法中执行相同的操作。

于 2013-01-04T15:03:33.863 回答
2

如果您要将 合并InitializeSimpleMembershipAttribute到 中,Global.asax.cs Application_Start以便在SimpleMembershipProvider不调用任何AccountController路由的情况下对其进行初始化...

...它可能看起来像这样:http ://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html

// 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);
                }
            }
        }
    }
}
于 2013-02-21T18:54:37.853 回答