ASP.NET Tutorial/Authentication Authorization/CreateUserWizard — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:57, 26 мая 2010
Содержание
Adding Steps to the CreateUserWizard Control
<%@ 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">
<div>
<asp:CreateUserWizard
id="CreateUserWizard1"
Runat="server" OnCreatedUser="CreateUserWizard1_CreatedUser">
<WizardSteps>
<asp:WizardStep>
<asp:Label
id="lblFirstName"
Text="First Name:"
AssociatedControlID="txtFirstName"
Runat="server" />
<br />
<asp:TextBox
id="txtFirstName"
Runat="server" />
<br /><br />
<asp:Label
id="lblLastName"
Text="Last Name:"
AssociatedControlID="txtLastName"
Runat="server" />
<br />
<asp:TextBox
id="txtLastName"
Runat="server" />
</asp:WizardStep>
<asp:CreateUserWizardStep />
</WizardSteps>
</asp:CreateUserWizard>
</div>
</form>
</body>
</html>
CreateUserWizard with Code Confirmation
<%@ 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">
<div>
<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>
</div>
</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">
<div>
Enter the confirmation code that you received by email.
<asp:Label
id="lblError"
EnableViewState="false"
ForeColor="Red"
Runat="server" />
<br /><br />
<asp:Label
id="lblUserName"
Text="User Name:"
AssociatedControlID="txtUserName"
Runat="server" />
<br />
<asp:TextBox
id="txtUserName"
Runat="server" />
<br /><br />
<asp:Label
id="lblConfirmationCode"
Text="Confirmation Code:"
AssociatedControlID="txtConfirmationCode"
Runat="server" />
<br />
<asp:TextBox
id="txtConfirmationCode"
Columns="50"
Runat="server" />
<asp:Button
id="btnConfirm"
Text="Confirm"
OnClick="btnConfirm_Click"
Runat="server" />
</div>
</form>
</body>
</html>
CreateUserWizard with Password Confirmation
<%@ 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">
<div>
<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>
</div>
</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>
Customizing the CreateUserWizard control with templates.
<%@ 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">
<div>
<asp:CreateUserWizard
id="CreateUserWizard1"
Runat="server">
<WizardSteps>
<asp:CreateUserWizardStep>
<ContentTemplate>
<h1>Register</h1>
<asp:Label
id="ErrorMessage"
ForeColor="Red"
Runat="server" />
<br/><br />
<asp:Label
id="lblUserName"
Text="User Name:"
AssociatedControlID="UserName"
Runat="server" />
<br />
<asp:TextBox
id="UserName"
Runat="server" />
<br /><br />
<asp:Label
id="lblPassword"
Text="Password:"
AssociatedControlID="Password"
Runat="server" />
<br />
<asp:TextBox
id="Password"
TextMode="Password"
Runat="server" />
<br /><br />
<asp:Label
id="lblEmail"
Text="Email:"
AssociatedControlID="Email"
Runat="server" />
<br />
<asp:TextBox
id="Email"
Runat="server" />
<br /><br />
<asp:Label
id="lblQuestion"
Text="Security Question:"
AssociatedControlID="Question"
Runat="server" />
<br />
<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>
<br /><br />
<asp:Label
id="lblAnswer"
Text="Security Answer:"
AssociatedControlID="Answer"
Runat="server" />
<br />
<asp:TextBox
id="Answer"
Runat="server" />
</ContentTemplate>
</asp:CreateUserWizardStep>
<asp:CompleteWizardStep>
<ContentTemplate>
Your account was successfully created.
</ContentTemplate>
</asp:CompleteWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
</div>
</form>
</body>
</html>
Sending a Create User Email Message
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">
<div>
<asp:CreateUserWizard
id="CreateUserWizard1"
Runat="server">
<MailDefinition
BodyFileName="Register.txt"
Subject="Registration Confirmation"
From="Admin@YourSite.ru" />
</asp:CreateUserWizard>
</div>
</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.
Turn off the Email
<%@ 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">
<div>
<asp:CreateUserWizard
id="CreateUserWizard1"
RequireEmail="false"
Runat="server" />
</div>
</form>
</body>
</html>
Using the CreateUserWizard Control
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">
<div>
<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" />
</div>
</form>
</body>
</html>
ContinueDestinationPageUrl is set to the value "~/Default.aspx".
After you successfully register, you are redirected to the Default.aspx page.