问题
I'm running into this error and I can't quite figure it out. I'm posting data to my controller via PostMan in Chrome, reaching the CreateUserAndAccount method, and receiving this error:
To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".
Here is my controller:
[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);
I'm using the same "InitializeSimpleMembershipAttribute" class as the MVC SPA template:
{
[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);
}
}
}
}
}
And in my web.config I have enabled simpleMembership:
<add key="enableSimpleMembership" value="true" />
What else could it be?
Edit: I just tried the tip found in this link:
http://insomniacgeek.com/to-call-this-method-the-membership-provider-property-must-be-an-instance-of-extendedmembershipprovider/
And I'm now getting
You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in an _AppStart.cshtml file in the root of your site.
Any suggestions would be awesome.
回答1:
I found the problem. I was referencing System.Web.MVC in my InitializeAttribute class... it should have been System.Web.Http.
来源:https://stackoverflow.com/questions/15822925/to-call-this-method-the-membership-provider-property-must-be-an-instance-of