views:

594

answers:

3

I'm having a rough time with this membership stuff.

OK, so, it's really odd. I can register a user. I can register, I can login. However, when I go to register ANOTHER user the user isn't saved in the database and I get a Membership credential verification failed event when the user tries to login (I assume because the user is never being saved).

Here is the code I am using to save a new user.

On the page:

protected void btnRegister_Click(object sender, EventArgs e)
 {
  if (false == ValidatePage()) return;
        FormsAuthentication.SignOut();
  MembershipCreateStatus status;
  Data.User user = UserManager.CreateUser(txtEmail.Text.Trim(), txtPassword.Text.Trim(), out status);

  switch (status)
  {
   case MembershipCreateStatus.Success:
    UserManager.Save(user);
    break;
   default:
    lblMessage.Text = status.ToString();
    break;
  }

        Response.Redirect("~/login.aspx");
 }

the CreateUser method:

public static User CreateUser(string username, string password, out MembershipCreateStatus status)
 {
  using (TransactionScope transaction = new TransactionScope())
  {
   MembershipUser aspnetUser = Membership.CreateUser(username, password, username, null, null, true, out status);

   User hqUser = null;

   if (status == MembershipCreateStatus.Success)
   {
    hqUser = new User();

    //these properties are only for issues
    //that won't blow up.  They can be safely removed from the system.
    //the aspnet membership tables take care of this stuff for us.
    hqUser.LastLoginDate = DateTime.Now;
    hqUser.DateCreated = DateTime.Now;
    //end properites.

    hqUser.Email = username;
    hqUser.MembershipID = (Guid)aspnetUser.ProviderUserKey;
    Save(hqUser);
   }

   transaction.Complete();
   return hqUser;

}}

The extra save method is for saving the user in the app's database. The user is not getting into the membership database though so I know it's dying before that.

anyone see anything obvious that's burning me? Thanks!

A: 

My first guess is that ValidatePage() return false.

Kon
no, because if that returns false you are notified on the page.
Sara Chipps
+2  A: 

Have you checked to see this is not a problem with password complexity? I know I have had issues with this in the past...

Geoffrey Chetwood
what did you use as a validator for the passwords?
Sara Chipps
@Sara: Um, not sure, but you should be able to change the complexities in your web.config. More info here: http://odetocode.com/Articles/427.aspx
Geoffrey Chetwood
ok, thanks again!
Sara Chipps
Heh, no problem!
Geoffrey Chetwood
What exactly is the reproduction scenario / solution here? I'm experiencing the same thing.
Jason Kealey
+1  A: 

You redirect the page before you are able to see the output if the status is not equal to success.

Btw. it might be better to inherit the sqlmembership provider and extend it with your own extra db inserts, at least if you are not inserting extra data. Or are you asking more than the default stuff on the first creation of the account?

Try it like this code and see what the value of the enum is in the lblMessage

protected void btnRegister_Click(object sender, EventArgs e) {

      if (!ValidatePage()) return;

        FormsAuthentication.SignOut();
            MembershipCreateStatus status;
            Data.User user = UserManager.CreateUser(txtEmail.Text.Trim(), txtPassword.Text.Trim(), out status);
            switch (status) { 
  case MembershipCreateStatus.Success:
                            UserManager.Save(user);
   Response.Redirect("~/login.aspx");
                            break;
                    default:
                            lblMessage.Text = status.ToString();
                            break;
            }       

    }

grrgr, stupid markup thing

Hope this helps.

erik