In "permission Logon", we introduced how to use Microsoft's latest permission features. By declaring simple permissions in the configuration file, we can control login users and anonymous users, it also teaches you how to use the Authentication event of the Login control to log on. However, you still need to complete the verification code in the event. As a fast-developed vs ide, does Microsoft provide us with simpler methods? The answer is "yes ". Today, we provide a complete page that provides simple login, registration, and password forgetting functions without writing any code. For comparison,
First, create a WebSite project (RequiredWebSiteOtherwise, data cannot be generated and built-in functions cannot be used!), And then create the following three pages: Default. aspx (a Login control), Register. aspx (A CreateUserWizard control), and ForgetPassword. aspx (a password forgetting control: PasswordRecovery ). Set Default to the Default start page and add the following code to web. config:
<Location path = "Default2.aspx">
<System. web>
<Authorization>
<Allow users = "*"/>
</Authorization>
</System. web>
</Location>
<Location path = "ForgetPassword. aspx">
<System. web>
<Authorization>
<Allow users = "*"/>
</Authorization>
</System. web>
</Location>
<System. web>
<Compilation debug = "true" targetFramework = "4.0"/>
<Authentication mode = "Forms">
<Forms path = "/" timeout = "60" slidingExpiration = "true" loginUrl = "Default. aspx"/>
</Authentication>
<Authorization>
<Allow users = "*"/>
<Deny users = "? "/>
</Authorization>
At the same time, convert your Login control to a Template mode (allow custom styles, such as adding some other controls), add a "Register" and "Forget Password" LinkButton, specify its PostBackUrl so that it can successfully jump to the registration and password forgetting page:
<Asp: Login runat = "server"
Onauthenticate = "Login1_Authenticate">
<LayoutTemplate>
<Table cellpadding = "1" cellspacing = "0" style = "border-collapse: collapse;">
<Tr>
<Td>
<Table cellpadding = "0">
<Tr>
<Td align = "center" colspan = "2">
Log In </td>
</Tr>
<Tr>
<Td align = "right">
<Asp: Label runat = "server" AssociatedControlID = "UserName"> User Name: </asp: Label>
</Td>
<Td>
<Asp: TextBox runat = "server"> </asp: TextBox>
<Asp: RequiredFieldValidator runat = "server"
ControlToValidate = "UserName" ErrorMessage = "User Name is required ."
ToolTip = "User Name is required." ValidationGroup = "Login1"> * </asp: RequiredFieldValidator>
</Td>
</Tr>
<Tr>
<Td align = "right">
<Asp: Label runat = "server" AssociatedControlID = "Password"> Password: </asp: Label>
</Td>
<Td>
<Asp: TextBox runat = "server" TextMode = "Password"> </asp: TextBox>
<Asp: RequiredFieldValidator runat = "server"
ControlToValidate = "Password" ErrorMessage = "Password is required ."
ToolTip = "Password is required." ValidationGroup = "Login1"> * </asp: RequiredFieldValidator>
</Td>
</Tr>
<Tr>
<Td colspan = "2">
<Asp: CheckBox runat = "server" Text = "Remember me next time."/>
</Td>
</Tr>
<Tr>
<Td align = "center" colspan = "2" style = "color: Red;">
<Asp: Literal runat = "server" EnableViewState = "False"> </asp: Literal>
</Td>
</Tr>
<Tr>
<Td align = "left">
<Asp: Button runat = "server" CommandName = "Login" Text = "Log In"
ValidationGroup = "Login1"/>
</Td>
<Td align = "right">
<Asp: LinkButton runat = "server" PostBackUrl = "~ /Register. aspx "> Register Now </asp: LinkButton>
& Nbsp;
<Asp: LinkButton runat = "server"
PostBackUrl = "~ /ForgetPassword. aspx "> Forget Password </asp: LinkButton>
</Td>
</Tr>
</Table>
</Td>
</Tr>
</Table>
</LayoutTemplate>
</Asp: Login>
Put a CreateUserWizard control in Register. Put a PasswordRecovery control on the ForgetPassword page. Add the following configuration in web. config (located outside the <system. web> node ):
<System.net>
<MailSettings>
<Smtp from = "sender address xxx@abc.com" deliveryMethod = "Network">
<Network host = "smtp server address (smtp. xx. xx)" userName = "Logon userName" password = "password" defaultCredentials = "server verification required"/>
</Smtp>
</MailSettings>
</System.net>
The PasswordRecovery control is also defined as follows:
<Asp: PasswordRecovery runat = "server">
<MailDefinition Subject ="Email Subject"/>
</Asp: PasswordRecovery>
Basically, a multi-page "project" with a very simple registration function is complete. Simple. Next we will analyze its principles:
1. First, check Default. aspx page section: if it is a WebSite project, when you place a Login control, it will be in a special folder named "APP_DATA" in the root directory (if you cannot see it, right-click the project, select Add ASP.. NET folder, select) Create an ASPNETDB. MDF database file. Related tables are created for subsequent user registration.
2. In Register. the aspx page contains a CreateuserWizard control that triggers the CreatingUser event and calls Membership. the CreateUser built-in class writes information to the automatically generated database one by one. Its function declaration is as follows:
Public static MembershipUser CreateUser (string username, string password, string email, string passwordQuestion, string passwordAnswer,Bool isApproved,Out MembershipCreateStatus status);
Other parameters can be learned one by one using English names, so I will not be embarrassed. Here, two special parameters should be noted: "isApproved" indicates whether to disable the user; "MembershipCreateStatus" is an enumeration used to report the creation status (if successful, it will be Success ). You can use "e. CreateUserError" in the "CreateUserWizard1_CreateUserError" event to obtain the failed information and perform special processing.
3. On the ForgetPassword. aspx page, enter a user name. In the VerfyingUser event, the system calls Membership. GetUser (string username) to obtain an instance of MembershipUser. If it does not exist, it indicates an error. e. Cancel is automatically set to true (Cancel the next operation ). If yes, then e. cancel = false to go to the next step. Get the Answer of the MemberShipUser (which does not directly provide attribute access) and compare it with your input. Then, send an email (automatically reading the content in the <system.net> node, corresponding code ):
SmtpClient s = new SmtpClient ();
MailMessage MS = new MailMessage ();
Ms. From = new MailAddress ("sender ");
Ms. To. Add (new MailAddress ("receiver "));
Ms. Subject = "Email Subject ";
Ms. Body = "email Body ";
S. DeliveryMethod = SmtpDeliveryMethod. Network; // sending Method
S. Host = "smtp. xxx. xxx ";
S. Credentials = new NetworkCredential ("sender email username", "password ");
S. Send (MS );