ASP.NET Tutorial/Authentication Authorization/CreateUserWizard
Содержание
Adding Steps to the CreateUserWizard Control
<source lang="csharp">
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Web.Configuration" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { CreateUserProfile(CreateUserWizard1.UserName,txtFirstName.Text,txtLastName.Text); } private void CreateUserProfile(string userName, string firstName, string lastName) { string conString = WebConfigurationManager.ConnectionStrings ["UserProfiles"].ConnectionString; SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand("INSERT UserProfiles (UserName,FirstName,LastName) VALUES (@UserName,@FirstName, cmd.Parameters.AddWithValue("@UserName", userName); cmd.Parameters.AddWithValue("@FirstName", firstName); cmd.Parameters.AddWithValue("@LastName", lastName); using (con) { con.Open(); cmd.ExecuteNonQuery(); } }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>CreateUserWizard Extra</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" Runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser"> <WizardSteps> <asp:WizardStep> <asp:Label id="lblFirstName" Text="First Name:" AssociatedControlID="txtFirstName" Runat="server" />
<asp:TextBox id="txtFirstName" Runat="server" />
<asp:Label id="lblLastName" Text="Last Name:" AssociatedControlID="txtLastName" Runat="server" />
<asp:TextBox id="txtLastName" Runat="server" /> </asp:WizardStep> <asp:CreateUserWizardStep /> </WizardSteps> </asp:CreateUserWizard>
</form>
</body> </html></source>
CreateUserWizard with Code Confirmation
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
protected void CreateUserWizard1_SendingMail(object sender, MailMessageEventArgs e) { MembershipUser user = Membership.GetUser(CreateUserWizard1.UserName); string code = user.ProviderUserKey.ToString(); e.Message.Body = e.Message.Body.Replace("<%ConfirmationCode%>", code); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>CreateUserWizard Code Confirmation</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" CompleteSuccessText="A confirmation email containing your new password has been sent to your email address." DisableCreatedUser="true" ContinueDestinationPageUrl="~/ConfirmCode.aspx" OnSendingMail="CreateUserWizard1_SendingMail" Runat="server"> <MailDefinition From="Admin@YourSite.ru" BodyFileName="CodeConfirmation.htm" IsBodyHtml="true" Subject="Registration Confirmation" /> </asp:CreateUserWizard>
</form>
</body> </html>
File: CodeConfirmation.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head>
<title>Code Confirmation</title>
</head> <body> <%UserName%>, your confirmation code is <%ConfirmationCode%> </body> </html> File: ConfirmCode.aspx Code View: Scroll / Show All <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
protected void btnConfirm_Click(object sender, EventArgs e) { MembershipUser user = Membership.GetUser(txtUserName.Text); if (user == null) { lblError.Text = "Invalid User Name"; } else { string providerCode = user.ProviderUserKey.ToString(); string userCode = txtConfirmationCode.Text.Trim(); if (providerCode != userCode) { lblError.Text = "Invalid Confirmation Code"; } else { user.IsApproved = true; Membership.UpdateUser(user); Response.Redirect("~/SecretFiles/Secret.aspx"); } } }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Confirm Code</title>
</head> <body>
<form id="form1" runat="server">
Enter the confirmation code that you received by email. <asp:Label id="lblError" EnableViewState="false" ForeColor="Red" Runat="server" />
<asp:Label id="lblUserName" Text="User Name:" AssociatedControlID="txtUserName" Runat="server" />
<asp:TextBox id="txtUserName" Runat="server" />
<asp:Label id="lblConfirmationCode" Text="Confirmation Code:" AssociatedControlID="txtConfirmationCode" Runat="server" />
<asp:TextBox id="txtConfirmationCode" Columns="50" Runat="server" /> <asp:Button id="btnConfirm" Text="Confirm" OnClick="btnConfirm_Click" Runat="server" />
</form>
</body> </html></source>
CreateUserWizard with Password Confirmation
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>CreateUserWizard Password Confirmation</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" CompleteSuccessText="A confirmation email containing your new password has been sent to your email address." AutoGeneratePassword="true" LoginCreatedUser="false" ContinueDestinationPageUrl="~/Login.aspx" Runat="server"> <MailDefinition From="Admin@YourSite.ru" BodyFileName="PasswordConfirmation.htm" IsBodyHtml="true" Subject="Registration Confirmation" /> </asp:CreateUserWizard>
</form>
</body> </html> File: PasswordConfirmation.htm <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head>
<title>Password Confirmation</title>
</head> <body>
Your new password is <% Password %>.
</body> </html></source>
Customizing the CreateUserWizard control with templates.
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>CreateUserWizard Template</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" Runat="server"> <WizardSteps> <asp:CreateUserWizardStep> <ContentTemplate>
Register
<asp:Label id="ErrorMessage" ForeColor="Red" Runat="server" />
<asp:Label id="lblUserName" Text="User Name:" AssociatedControlID="UserName" Runat="server" />
<asp:TextBox id="UserName" Runat="server" />
<asp:Label id="lblPassword" Text="Password:" AssociatedControlID="Password" Runat="server" />
<asp:TextBox id="Password" TextMode="Password" Runat="server" />
<asp:Label id="lblEmail" Text="Email:" AssociatedControlID="Email" Runat="server" />
<asp:TextBox id="Email" Runat="server" />
<asp:Label id="lblQuestion" Text="Security Question:" AssociatedControlID="Question" Runat="server" />
<asp:DropDownList id="Question" Runat="server"> <asp:ListItem Text="Enter the name of your pet" Value="Pet Name" /> <asp:ListItem Text="Enter your favorite color" Value="Favorite Color" /> </asp:DropDownList>
<asp:Label id="lblAnswer" Text="Security Answer:" AssociatedControlID="Answer" Runat="server" />
<asp:TextBox id="Answer" Runat="server" /> </ContentTemplate> </asp:CreateUserWizardStep> <asp:CompleteWizardStep> <ContentTemplate> Your account was successfully created. </ContentTemplate> </asp:CompleteWizardStep> </WizardSteps> </asp:CreateUserWizard>
</form>
</body> </html></source>
Sending a Create User Email Message
<source lang="csharp">
You can set up the CreateUserWizard control to automatically send an email when a new user registers. The Email can contain the new user"s registered username and password. <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>CreateUserWizard Email</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" Runat="server"> <MailDefinition BodyFileName="Register.txt" Subject="Registration Confirmation" From="Admin@YourSite.ru" /> </asp:CreateUserWizard>
</form>
</body> </html> File: Register.txt Thank you for registering!
Here is your new username and password:
username: <% UserName %> password: <% Password %>
When the email is sent, the user"s registered username and password are substituted for these expressions.</source>
Turn off the Email
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>CreateUserWizard Short</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" RequireEmail="false" Runat="server" />
</form>
</body> </html></source>
Using the CreateUserWizard Control
<source lang="csharp">
The CreateUserWizard control renders a user registration form. If a user successfully submits the form, then a new user is added to your website. In the background, the CreateUserWizard control uses ASP.NET membership to create the new user. File: ShowCreateUserWizard.aspx <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Show CreateUserWizard</title>
</head> <body>
<form id="form1" runat="server">
<asp:CreateUserWizard id="CreateUserWizard1" ContinueDestinationPageUrl="~/Default.aspx" InstructionText="Please complete the following form." CompleteSuccessText="Your new account has been created. Thank you for registering." CssClass="createUser" TitleTextStyle-CssClass="createUser_title" InstructionTextStyle-CssClass="createUser_instructions" CreateUserButtonStyle-CssClass="createUser_button" ContinueButtonStyle-CssClass="createUser_button" Runat="server" />
</form>
</body> </html>
ContinueDestinationPageUrl is set to the value "~/Default.aspx".
After you successfully register, you are redirected to the Default.aspx page.</source>