我遇到了這個錯誤,我無法弄清楚它。我在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