Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 9th, 2010, 09:46 PM
Registered User
 
Join Date: Jun 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default problem creating user

Hi,

I an developing an application that is based on the beerhouse.
Because of accessibility issues I've had to modify the registration process.
Instead of using the createuserwizard, I'm manually creating users using a two step wizard control. In the first step I pull in username,pasword,email ect validate and add the submitted fields via the following code -
protectedvoid SignUpButton_Click(object sender, EventArgs e)
{
Wizard1.StepNextButtonStyle.CssClass =
"hidden";
if (!Page.IsValid)
{
return;
}
Wizard1.ActiveStepIndex = Wizard1.ActiveStepIndex + 1;
MembershipCreateStatus createStatus;
MembershipUser newUser = Membership.CreateUser(UserNameTextBox.Text, PasswordTextBox.Text, EmailTextBox.Text, QuestionTextBox.Text, AnswerTextBox.Text, true, out createStatus);
switch (createStatus)
{
caseMembershipCreateStatus.Success:
CreateAccountResults.Text =
"The user account was successfully created!";
break;
caseMembershipCreateStatus.DuplicateUserName:
CreateAccountResults.Text =
"There already exists a user with this username.";
break;
caseMembershipCreateStatus.DuplicateEmail:
CreateAccountResults.Text =
"There already exists a user with this email address.";
break;
caseMembershipCreateStatus.InvalidEmail:
CreateAccountResults.Text =
"There email address you provided in invalid.";
break;
caseMembershipCreateStatus.InvalidAnswer:
CreateAccountResults.Text =
"There security answer was invalid.";
break;
caseMembershipCreateStatus.InvalidPassword:
CreateAccountResults.Text =
"The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
break;
default:
CreateAccountResults.Text =
"There was an unknown error; the user account was NOT created.";
break;
}
Roles.AddUserToRole(newUser.UserName, "Posters");
}

In the second step I then include the userprofile control -
<
mb:UserProfileID="UserProfile1"runat="server"/>

The problem arises with the userprofile postback. The following code used is unable to recognise the newly created user -

ProfileCommon profile = this.Profile;
if (this.UserName.Length > 0)
profile =
this.Profile.GetProfile(this.UserName);
ddlSubscriptions.SelectedValue = profile.Preferences.Newsletter.ToString();
ect...

The this.Profile is deemed anonymous!

Is this because the controls viewstate is being incorrectly configured? Does the use of the createuzerwizard save the username to the control's viewstate and am I not accounting for this when manually creating users?

Please help.
 
Old March 10th, 2010, 12:51 AM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by riffraff View Post
Does the use of the createuzerwizard save the username to the control's viewstate and am I not accounting for this when manually creating users?

You guessed it exactly. The CreateUserWizard maintains a reference to the user name of the user it creates through its UserName property. Since you aren't using the CreateUserWizard, you won't have access to that property.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 10th, 2010, 11:50 AM
Registered User
 
Join Date: Jun 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.

In order for the username to persist throughout the wizard steps, do I have to save this information to the wizards control state? If so, I would be greatful if you could provide any pointers to a possible solution?
 
Old March 10th, 2010, 12:31 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

There are several ways you can go here.

I probably wouldn't bother trying to mess with the ControlState. There are easier state-control mechanisms you could use here, like Session. After the user is created, you could store the UserName in Session, then retrieve it in the next step.

Another way to go would be to log the person in right away after they are created. You would use the Forms Authentication API to set the auth cookie in the create step. Then, on the next step, the response will send the cookie, and the user will be recognized as an authenticated user. This is essentially what the CreateUserWizard does by default (behind the scenes of course).

If you examine the UserProfile control, it is set up to automatically pull the profile of the currently authenticated user, so it should be ready to go then.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old March 10th, 2010, 02:20 PM
Registered User
 
Join Date: Jun 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default problem authenticating

Hi Lee,
You have been a great help, but I now have another problem ?

I decided to follow your advise of using the Forms API to set the cookie after creating the user as seen in the following code -
Wizard1.ActiveStepIndex = Wizard1.ActiveStepIndex + 1;
MembershipCreateStatus createStatus;
MembershipUser newUser = Membership.CreateUser(UserNameTextBox.Text, PasswordTextBox.Text, EmailTextBox.Text, QuestionTextBox.Text, AnswerTextBox.Text, true, out createStatus);
switch (createStatus)
{
caseMembershipCreateStatus.Success:
CreateAccountResults.Text =
"The user account was successfully created!";
break;
caseMembershipCreateStatus.DuplicateUserName:
CreateAccountResults.Text =
"There already exists a user with this username.";
break;
caseMembershipCreateStatus.DuplicateEmail:
CreateAccountResults.Text =
"There already exists a user with this email address.";
break;
caseMembershipCreateStatus.InvalidEmail:
CreateAccountResults.Text =
"There email address you provided in invalid.";
break;
caseMembershipCreateStatus.InvalidAnswer:
CreateAccountResults.Text =
"There security answer was invalid.";
break;
caseMembershipCreateStatus.InvalidPassword:
CreateAccountResults.Text =
"The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
break;
default:
CreateAccountResults.Text =
"There was an unknown error; the user account was NOT created.";
break;
}
if (Membership.ValidateUser(newUser.UserName, PasswordTextBox.Text))
{
bool test = FormsAuthentication.Authenticate(newUser.UserName, PasswordTextBox.Text);
Roles.AddUserToRole(newUser.UserName, "Posters");
if (test) Label2.Text = "<p>User Successfully Authenticated</p>";
else Label2.Text = "<p>User Unsuccessfully Authenticated</p>";
}

The problem I'm having is that when I pass the paramaters - newUser.UserName, PasswordTextBox.Text to the FormsAuthentication.Authenticate() method, I get a false bool return value, thus the authentication fails. This is strange since I use the same values in the Membership.ValidateUser() method, in the if statement above, which returns a true value?

Your thoughts would be appreciated.
 
Old March 10th, 2010, 02:55 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Authenticate is not the correct method to use. Try SetAuthCookie instead.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
riffraff (March 10th, 2010)
 
Old March 10th, 2010, 05:44 PM
Registered User
 
Join Date: Jun 2009
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

That did the trick - thanks again for all your help!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch.14 Creating Windows Form User Controls problem SAIFI BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 1 September 29th, 2008 05:51 AM
problem with creating membership user hertendreef ASP.NET 2.0 Basics 1 April 9th, 2007 02:37 PM
WebShop : problem of creating new user JoinTTT BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 April 6th, 2007 06:22 PM
get an error when creating a new user...?? riki_gill ASP.NET 1.x and 2.0 Application Design 1 October 29th, 2006 01:27 PM
Problem creating user-level security JackNimble Access 2 October 11th, 2004 11:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.