我遇到了这个错误,我无法弄清楚它。我在Chrome通过邮递员发布的数据,以我的控制器,达到CreateUserAndAccount方法,并且收到此错误:要调用此方法,“Membership.Provider”属性必须是“ExtendedMembershipProvider”的一个实例。
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);
我使用的是相同的“InitializeSimpleMembershipAttribute “类作为MVC SPA模板:
{
[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" />
还能有什么?
编辑:我只是想在这个环节发现提示:
而现在我越来越
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.
任何建议将真棒。
你能解释一下吗?当我改变对Http的引用时,它将不再编译。 Http和MVC中正在引用什么? – vbullinger 2013-06-03 14:27:17
@vbullinger检查您的参考(右键单击>添加参考)并确保system.web.http包含在您的项目中。也看看MSDN文档,如果你需要更多地了解内脏 – SB2055 2013-06-04 00:53:27
真的很糟糕的解释。 – Kyberias 2014-11-03 13:33:12