How To Game
How To Game

Reputation: 91

How to obtain a parameter value from a URL to a web form in asp.net?

I have a problem on how to get the exact person id with email address to do resetting password. So, from here I will send a hyperlink to the user via email.

            SmtpClient client = new SmtpClient();

            try
            {
                SendPasswordResetEmail(u.EmailAddress.ToString(),u.Name.ToString(),UniqueID);
                lblMessage.ForeColor = System.Drawing.Color.LimeGreen;
                lblMessage.Text = "Email has been successfully sent!";
            }
            catch (Exception exc)
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
                lblMessage.Text = "ERROR: " + exc.Message;
            }

Then this is my message content to the user in order to let them click into the hyperlink.

        string emailAddress = txtEmail.Text;

        User u=db.Users.Single(x => 
                            x.EmailAddress == emailAddress);

        MailMessage mailMessage = new MailMessage(u.EmailAddress, ToEmail);
        StringBuilder sbEmailBody = new StringBuilder();
        sbEmailBody.Append("Dear " + u.Name.ToString() + ",<br/><br/>");
        sbEmailBody.Append("Please click on the following link to reset your password");
        sbEmailBody.Append("<br/>"); sbEmailBody.Append("https://localhost:44305/Registration/ChangePwd.aspx?uid=" + u.Id);
        sbEmailBody.Append("<br/><br/>");
        sbEmailBody.Append("<b>吹水站</b>");

        mailMessage.IsBodyHtml = true;

        mailMessage.Body = sbEmailBody.ToString();
        mailMessage.Subject = "Reset Your Password";
        SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);

        MailMessage msg = new MailMessage();

        msg.To.Add(new MailAddress(txtEmail.Text));
        smtpClient.EnableSsl = true;
        smtpClient.Send(mailMessage);

Then this is my ChangePwd.aspx?uid= pages

<form id="form1" runat="server">
    <div>
        <h1>Change password</h1>
    </div>
    <div>
        <asp:Literal ID="litEmailAddress" runat="server"></asp:Literal>
    </div>
    <div>
        <p>Enter new password : 
        <asp:TextBox ID="txtNewPwd" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please fill in the blank" ControlToValidate="txtNewPwd" CssClass="error" Display="Dynamic"></asp:RequiredFieldValidator>
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtNewPwd" CssClass="error" ErrorMessage="Password must be between 8-16 characters including at least 1 letter and 1 number" ValidationExpression="((?=.*\d)(?=.*[a-z]).{8,16})"></asp:RegularExpressionValidator>
        </p>
    </div>
    <div>
        Re-enter new password:
        <asp:TextBox ID="txtRepeatPwd" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please fill in the blank" ControlToValidate="txtRepeatPwd" CssClass="error" Display="Dynamic"></asp:RequiredFieldValidator>
        <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtNewPwd" ControlToValidate="txtRepeatPwd" CssClass="error" ErrorMessage="Password and repeat password are not matched"></asp:CompareValidator>
    </div>
    <p>
    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CausesValidation="False" PostBackUrl="~/Registration/Login.aspx" /><asp:Button ID="btnConfirm" runat="server" Text="Confirm" OnClick="btnConfirm_Click" />
    </p>
    <div>
    <p><asp:Label ID="lblSuccess" runat="server" Text=""></asp:Label></p>
    <asp:Label ID="lblFailed" runat="server" Text=""></asp:Label>
    </div>

</form>

From here, I don't know how to get the exact user email address when they clicked the hyperlink to ChangePwd.aspx to do resetting. And then, I have to update the database to store the updated changed password for the user. I tried to search online but lack of resources I got from the internet. Anyone please help thank you.

Upvotes: 0

Views: 1493

Answers (2)

Swinkaran
Swinkaran

Reputation: 1255

When the ChangePwd.aspx page is loaded you need to identify the user uniqely so that you can save the new password to that specific user. What you need here is to pass the primary id(either user Id or email address) from your email to the ChangePwd.aspx page. Which you are doing correctly by passing the ID to the Urls query string,

https://localhost:44305/Registration/ChangePwd.aspx?uid=" + u.Id);

Now, You need to read this in your ChangePwd.aspx.cs (the serverside class of the WebForm). You can do something like this in the page_load method,

protected void Page_Load(object sender, EventArgs e)
{
   string userId= Request.QueryString["uid"];
   // get the user *User* from the database, using the above retrieved ID

   litEmailAddress.text = User.email; // something like this
}

Now, you can save the new password to this user. Let me know if this helps or need more help.

Upvotes: 1

Paul Wheeler
Paul Wheeler

Reputation: 20140

You should be able to use the Request.QueryString property to access the value of uid specified in the URL (i.e. Request.QueryString["uid"]). It should then be straightforward to fetch the User object with that Id and get the user's email address.

Here is a basic tutorial: https://www.c-sharpcorner.com/UploadFile/ca2535/query-string-in-Asp-Net/

Upvotes: 0

Related Questions