3

我遇到了这个错误,我无法弄清楚。我正在通过 Chrome 中的 PostMan 将数据发布到我的控制器,到达 CreateUserAndAccount 方法,并收到此错误:

To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".

这是我的控制器:

[System.Web.Http.Authorize]
[InitializeSimpleMembership]
public class AccountController : ApiController
{
        // POST: /api/register
        [System.Web.Http.HttpPost]
        [System.Web.Http.AllowAnonymous]
        [ValidateAntiForgeryToken]
        //public HttpResponseMessage Register(RegisterModel model, string returnUrl)
        public HttpResponseMessage Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password); // blows up here
                    WebSecurity.Login(model.UserName, model.Password);

我使用与 MVC SPA 模板相同的“InitializeSimpleMembershipAttribute”类:

{
    [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);
                }
            }
        }
    }
}

在我的 web.config 中,我启用了 simpleMembership:

<add key="enableSimpleMembership" value="true" />

还能是什么?

编辑:我刚刚尝试了此链接中的提示:

http://insomniacgeek.com/to-call-this-method-the-membership-provider-property-must-be-an-instance-of-extendedmembershipprovider/

我现在得到

在调用“WebSecurity”类的任何其他方法之前,您必须调用“WebSecurity.InitializeDatabaseConnection”方法。此调用应放置在站点根目录中的 _AppStart.cshtml 文件中。

任何建议都会很棒。

4

1 回答 1

-1

我发现了问题。我在我的 InitializeAttribute 类中引用了 System.Web.MVC……它应该是 System.Web.Http。

于 2013-04-04T23:21:42.387 回答