View Single Post
  #1 (permalink)  
Old February 10th, 2012, 03:46 PM
hozdaman hozdaman is offline
Authorized User
Points: 221, Level: 4
Points: 221, Level: 4 Points: 221, Level: 4 Points: 221, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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>
Reply With Quote