Encountering error 'The Provider encountered an unknown error' while trying WebSecurity.CreateAccount in asp.net webpage
Asked Answered
H

4

10

I am new to asp.net. I am trying to create a simple login and register webpage with WebMatrix. But I get the following error when I try to create an account:


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

Any help is appreciated. Thanks.

Hutchison answered 18/1, 2013 at 5:52 Comment(1)
Maybe the problem is in your InitializeDatabaseConnection method. Could you post it?Consummation
Q
4

Be sure to:

  1. Create the User record first
  2. Invoke WebMatrix to create the User account.

Below is my code which resolved the same issue you're having:

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");
        }

Also, you don't want to Respoonse.Redirect to a .cshtml file. Instead you should return a RedirectToAction and pass in the Action and Controller names.

Quiddity answered 14/2, 2013 at 17:54 Comment(0)
R
5

I just had the same issue ... I was using

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

and above generated issue ..

then I tried:

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

and find out that I was missing other required fields of my Users table (UserProfile). Following worked for me:

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

The post is old, but hope this can help others..

Roderick answered 24/10, 2013 at 10:27 Comment(0)
Q
4

Be sure to:

  1. Create the User record first
  2. Invoke WebMatrix to create the User account.

Below is my code which resolved the same issue you're having:

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");
        }

Also, you don't want to Respoonse.Redirect to a .cshtml file. Instead you should return a RedirectToAction and pass in the Action and Controller names.

Quiddity answered 14/2, 2013 at 17:54 Comment(0)
R
0

I've just had this problem - but by your code it might not be the same thing. I was trying to create the membership record before creating the record in my own user table. It has to be the other way round.

So I'd check that your INSERT INTO UserData query was actually working.

River answered 6/2, 2013 at 11:4 Comment(0)
M
0

In my case; It was because of Culture;

the Membership provider for creating account with CreateUserAndAccount function, first create a user. it is ok for now, and the user successfully added to the Database. But for creating account, It runs the below query to get userID to create new account for it:

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

Here is where exception thrown because in some culture upper casing the letters is different, for example in turkish, the upper case for 'i' is 'İ'.

I Don't know how to solve this problem for this moment, and I'll try to learn it.

In my case I was creating a user named "admin" and exception throw. I change the user name to "mesut" And it runs successfully.

please make sure if you have a culture specific letters in userName field.(or email in your case), (as soon as I found how to solve it I will post it here)

Mastic answered 12/2, 2014 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.