2013-01-18 26 views
8

我是asp.net新手。我正嘗試使用WebMatrix創建一個簡單的登錄和註冊網頁。但我得到以下錯誤,當我嘗試創建一個帳戶:在asp.net網頁中嘗試WebSecurity.CreateAccount時遇到錯誤'提供者遇到未知錯誤'


The Provider encountered an unknown error. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.Security.MembershipCreateUserException: The Provider encountered an unknown error. 

Source Error: 


Line 32:     db.Execute("INSERT INTO UserData (Email,Name) VALUES (@0,@1)", email, username); 
Line 33:     
Line 34:     WebSecurity.CreateAccount(email,password); 
Line 35:     Response.Redirect("Homepage.cshtml"); 
Line 36:    } 


Source File: c:\Users\admin\Documents\My Web Sites\Login\Register.cshtml Line: 34 

Stack Trace: 


[MembershipCreateUserException: The Provider encountered an unknown error.] 
    WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName, String password, Boolean requireConfirmationToken) +1312 
    WebMatrix.WebData.WebSecurity.CreateAccount(String userName, String password, Boolean requireConfirmationToken) +31 
    ASP._Page_Register_cshtml.Execute() in c:\Users\admin\Documents\My Web Sites\Login\Register.cshtml:34 
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207 
    System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68 
    System.Web.WebPages.WebPage.ExecutePageHierarchy() +156 
    System.Web.WebPages.StartPage.RunPage() +19 
    System.Web.WebPages.StartPage.ExecutePageHierarchy() +65 
    System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76 
    System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119 

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

任何幫助表示讚賞。謝謝。

+0

也許問題出在你的InitializeDatabaseConnection方法。你可以發佈嗎? – GmG

回答

4

務必:

  1. 創建用戶記錄第一
  2. 調用WebMatrix中創建用戶帳戶。

下面是我的代碼,從而解決您遇到了同樣的問題:

public ActionResult SignUp(SignUpModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      using (MorphometryContext context = new MorphometryContext()) 
      { 
       User user = new User 
       { 
        Email = model.Email, 
        FirstName = model.FirstName, 
        LastName = model.LastName, 
        Username = model.UserName 
       }; 
       context.Users.Add(user); 
       context.SaveChanges(); 
      } 

      // Attempt to register the user 
      try 
      { 
       WebSecurity.CreateAccount(model.UserName, model.Password); 
      } 
      catch (MembershipCreateUserException e) 
      { 
       ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); 
       return View(); 
      } 

      WebSecurity.Login(model.UserName, model.Password); 
      return RedirectToAction("Index", "Projects"); 
     } 

而且,你不想Respoonse.Redirect到.cshtml文件。相反,您應該返回一個RedirectToAction並傳入Action和Controller名稱。

0

我剛剛遇到了這個問題 - 但通過你的代碼,它可能不是一回事。我正在創建成員記錄,然後在我自己的用戶表中創建記錄。它必須是相反的。

所以我會檢查你的INSERT INTO UserData查詢是否真正起作用。

4

我有同樣的問題......我用

WebSecurity.CreateAccount(model.UserName, model.Password); 

及以上產生的問題..

然後我想:

WebSecurity.CreateAccountAndUser(model.UserName, model.Password); 

,並找出我是缺少我的用戶表中的其他必填字段(UserProfile)。下面爲我​​工作:

WebSecurity.CreateAccountAndUser(model.UserName, model.Password, new {RequiredColumn1 = Value1, RequiredColumn2 = Value 2, ...... }); 

的職位是舊的,但希望這可以幫助別人..

0

在我的情況;這是因爲文化;

用於創建具有CreateUserAndAccount函數的帳戶的成員資格提供程序,首先創建一個用戶。現在可以,用戶成功添加到數據庫。 但對於創建帳戶,它運行下面的查詢來獲取用戶ID爲它創建新帳戶:

SELECT [userID] from [User] Where (UPPER([userName]) = @0); 

這裏是例外,在土耳其在某些文化上殼體的字母不同,是因爲拋出,例如, '我'的大寫字母是'İ'。

我不知道如何解決這個問題,現在我試着去學習它。

在我的情況下,我創建了一個名爲「admin」和異常拋出的用戶。我將用戶名更改爲「mesut」併成功運行。

請確保如果你有一個特定的文化字母用戶名外地。(或電子郵件你的情況),(只要我發現如何解決這個問題,我將它張貼在這裏)

相關問題