Wrox Programmer Forums
|
BOOK: Beginning ASP.NET Web Pages with WebMatrix
This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix 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 February 10th, 2012, 04:46 PM
Authorized User
 
Join Date: Aug 2010
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default Chapter 12:Creating Registration Form

I am having problems with the registration/login form.

I can get through the entire registration process, as well as confirming the account.

When i go to login. The page does not let me login i keep getting a password error.

Here is my registration code

Code:
@{
    Page.Title="Create an account";
    var sql = string.Empty;
    var firstname = Request["firstname"];
    var lastname = Request["lastname"];
    var username = Request["username"];
    var password = Request["password"];
    var email = Request["email"];
    var email2 = Request["email2"];
    
    if(IsPost){
        if(firstname.IsEmpty()){
            ModelState.AddError("firstname", "Please provide your first name");
        }
        if(lastname.IsEmpty()){
            ModelState.AddError("lastname", "Please provide your last name");
        }
        if(username.IsEmpty()){
            ModelState.AddError("username", "You must provide a user name");
        }
        if(password.IsEmpty()){
            ModelState.AddError("password", "You must provide a password");
        }
        if(email.IsEmpty()){
            ModelState.AddError("email", "You must provide an email address");
        }
        if(!email.IsEmpty() && !Functions.IsValidEmail(email)){
            ModelState.AddError("email", "Please provide a valid email address");
        }
        if(email2 != email){
            ModelState.AddError("email2", "Email addresses must match");
        }
        if(!ModelState.IsValid){
            ModelState.AddFormError(@"Please fix the errors below before resubmitting form");
        }
        else{
            var db = Database.Open("Classifieds");
            var user = new{FirstName = firstname, LastName=lastname, Email=email};
            try{
                var token = WebSecurity.CreateUserAndAccount(username, password, user, true);
                var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
                var queryString = HttpUtility.UrlEncode(token);
                var confirm = "~/Account/Confirm?confirmationCode=";
                var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute(confirm + queryString);
                var message = "<p>Your confirmation code is: "  + token + "</p>" + "<p>Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a>" + " to activate your account.</p>";
                WebMail.Send(
                    to: email,
                    subject: "Please confirm your account",
                    body: message,
                    isBodyHtml: true
                    );
                Response.Redirect("~/Account/Thanks");
            }
            catch(MembershipCreateUserException ex){
                if(ex.StatusCode == MembershipCreateStatus.DuplicateUserName){
                    ModelState.AddError("username", "That user name already exists. Please select another one.");
                    ModelState.AddFormError(@"Please fix the errors below before resubmitting the form");
                }else{
                    ModelState.AddFormError("Something went wrong. Please try again");
                }
            }
        }
    }
}
<h2>Create an account</h2>
<p>Use the form below to create an account with this site.  Then you can post item for sale and bid on other items.
    Please make sure that you complete all fields marked with an asterisk *</p>
<form method = "post">
    <fieldset>
        <legend>Register</legend>
        
        @Html.ValidationSummary(true)
        <div>
            <label for="firstname">First Name*</label>
        </div>
        <div>
            <input type="text" id="firstname" name="firstname" value="@firstname" />
            @Html.ValidationMessage("firstname")
        </div>
        <div>
            <label for="lastname">Last Name*</label>
        </div>
        <div>
            <input type="text" id="lastname" name="lastname" value="@lastname" />
            @Html.ValidationMessage("lastname")
        </div>
        <div>
             <label for="userame">User Name*</label>
        </div>
        <div>
            <input type="text" id="username" name="username" value="@username" />
            @Html.ValidationMessage("usernamee")
        </div>
        <div>
            <label for="password">Password*</label>
        </div>
        <div>
            <input type="password" id="password" name="password" />
            @Html.ValidationMessage("password")
        </div>
        <div>
            <label for="email">Email*</label>
        </div>
        <div>
            <input type="text" id="email" name="email" value="@email" />
            @Html.ValidationMessage("email")
        </div>
        <div>
            <label for="email2">Re-enter your email</label>
        </div>
        <div>
            <input type="text" id="email2" name="email2" value="@email2" />
            @Html.ValidationMessage("email2")
        </div>
        <div>
            <input type="submit" name="Submit" value="Register"
        </div> 
    </fieldset>
</form>
here is my login form
Code:
@{
    Page.Title = "Log In";
    
    var username = Request.Form["username"];
    var password = Request.Form["password"];
    var rememberMe = false;
    if (IsPost) {
        rememberMe = Request.Form["rememberMe"].AsBool();
        
        if (username.IsEmpty()) {
            ModelState.AddError("username", "You must specify a username.");
        }
        if (password.IsEmpty()) {
            ModelState.AddError("password", "You must provide your password.");
        }
        
        if (ModelState.IsValid){
            ModelState.AddFormError(@"Please fix the errors below before resubmitting the form");
        }
        else {
            if (WebSecurity.Login(username,password,rememberMe)) {
                var returnUrl = Request.QueryString["ReturnUrl"];
                if (returnUrl.IsEmpty()) {
                    Response.Redirect("~/");
                } else {
                    Context.RedirectLocal(returnUrl);
                }
            }
            else {
                ModelState.AddFormError(@"Your credentials did not match a valid account.  Please try again.");
            }
        }
    }
}
<p>
    Please enter your username and password below.  If you don't have an account, visit the <a href="@Href("~/Account/Register")">registration page</a> and create one.
</p>
<form method="post">
    <fieldset>
        <legend>Log into Your Account</legend>
        @Html.ValidationSummary(true)
        <div>
            <label for="username">Username*</label>
        </div>
        <div>
            <input type="text" id="username" name="username" value="@username" />
            @Html.ValidationMessage("username")
        </div>
        <div>
            <label for="password">Password*</label>
        </div>
        <div>
            <input type="password" id="password" name="password" />
            @Html.ValidationMessage("password")
        </div>
        <div>
            <input type="checkbox" name="rememberMe" value="true" @(rememberMe ? "checked=\"checked\"" : string.Empty) />
            <label for="rememberMe">Remember Me</label>
        </div>
        <div>
            <input type="submit" value="login" title="Login" />
        </div>
    
    </fieldset>
</form>
 
Old February 11th, 2012, 04:33 AM
Wrox Author
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

What error do you get?
 
Old February 11th, 2012, 06:45 AM
Authorized User
 
Join Date: Aug 2010
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default

When I create a new user and confirm the new user. I try and login using that account, but the password does not get accepted. I made sure i am typing the right password yet the login page won't let me login.
 
Old February 11th, 2012, 04:40 PM
Wrox Author
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

That doesn't sound like a programming problem. That sounds like you are entering an invalid user name or password.
 
Old February 11th, 2012, 04:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Are you sure the account confirmation works? Can you show us the code for the page that confirms the account? Also, have you looked in the database to see if the account has indeed been confirmed correctly?

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 11th, 2012, 05:54 PM
Authorized User
 
Join Date: Aug 2010
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Hi I checked the database and see 4 users I created.

Here is the code for the confirmation

Code:
@{
    Page.Title="Registration Confirmation Page";
    
    var message = string.Empty;
    var confirmationToken = Request["confirmationCode"];
    
    WebSecurity.Logout();
    if (!confirmationToken.IsEmpty()) {
        if (WebSecurity.ConfirmAccount(confirmationToken.Trim())) {
            message = @"Registration Confirmed!";
            message += "Click <a href=\"/Account/Login\">here</a> to log in to the site.";
        } else {
            message = "Could not confirm your registration info";
        }
    }
}

@if (!message.IsEmpty()) {
    <p>@Html.Raw(message)</p>
} else {
    <form method="post" action="">
        <fieldset>
            <legend>Confirmation Code</legend>
            <label for="confirmationCode">
                Please enter the confirmation code sent to you via email and the click the <em>Confirm</em> button.
            </label>
            <input type="text" id="confirmationCode" name="confirmationCode" />
            <input type="submit" value="Confirm" />
        </fieldset>
    </form>
}
 
Old February 11th, 2012, 05:57 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Created, but also confirmed?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 11th, 2012, 07:33 PM
Authorized User
 
Join Date: Aug 2010
Posts: 50
Thanks: 7
Thanked 0 Times in 0 Posts
Default

Yes I get the email for the confirmation then I click the token and it brings me to the following page:

Quote:
Your confirmation code is: rAqZGZvCiJk6RGm/8YL5Cw==

Visit http://localhost:25047/Account/Confi...2f8YL5Cw%3d%3d to activate your account.
when i click the link I get the following:
Quote:
Registration Confirmed!Click here to log in to the site.
I go to login put in the user name and password. And it gives me an error saying
Quote:
Please fix the errors below before resubmitting the form
But there is no errors.
 
Old February 11th, 2012, 07:44 PM
Wrox Author
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

Your code currently shows the error message if there are no errors, or when ModelState.IsValid. You need to change that line by adding ! before ModelState.IsValid:

if(!ModelState.IsValid){
ModelState.AddFormError(@"Please fix the errors below before resubmitting the form");
}
The Following User Says Thank You to Mike Brind For This Useful Post:
hozdaman (February 15th, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Registration form advice Clint Beginning PHP 2 September 16th, 2009 04:42 AM
Creating a registration form gertho1 BOOK: Beginning ASP.NET 2.0 and Databases 1 April 6th, 2009 02:16 PM
problem in registration form prashant_telkar ASP.NET 1.x and 2.0 Application Design 5 July 11th, 2007 01:09 AM
problem with trigger in registration form prashant_telkar SQL Server 2000 2 July 9th, 2007 06:52 PM
Registration Form Help invisible bunny king Classic ASP Databases 7 February 5th, 2007 05:30 PM





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