Asp.net| control asp.net 2.0, a number of new features and controls are added. Among them, the new landing controls make Web applications design more handy. What is a login control? Is that we usually use in the Web application of the user registration, login, forget the password, after landing according to the different rights and display different pages, and so on, now in ASP.net 2.0 can be implemented by the control provided. ASP.net 2.0 in the landing control more, encapsulated in most Web applications to achieve system landing series functions, involving many aspects, this article will briefly describe their main functions.
First, in ASP.net 2.0, a new framework membership is introduced in order to make it easier to create and manage users and password-protect pages in WEB applications. The new framework contains additional features to handle authentication and authorization that can meet the needs of both Web site administrators and developers. Web site administrators can leverage new Web site Administration tools to create new users and roles, and control access to pages in Web applications. The Web Site Administration tool is a set of predefined ASP. NET pages, users who do not have programming skills can use them to configure WEB applications. Using the membership API, programmers can easily use the drag control method, and then a small number of code, you can fully implement the user, role permissions, and so on, can also be customized to expand.
Among them, the membership framework contains two default user validators: The default AccessMembershipProvider and SqlMembershipProvider. If you want to store membership information in a Microsoft SQL Server database, you can configure your application to use SqlMembershipProvider, and if you want to use an Access database, The AccessMembershipProvider is used. You can also create custom membership providers. For example, if you need to store membership information in an XML file, or in an Oracle database, and you want to create your own membership provider, you need to implement all the methods and properties of the abstract class MembershipProvider. In this article, you'll take an Access database as an example, so use AccessMembershipProvider.
We can use the ASP.net 2.0 self-contained Web site Security Configuration tool to automatically create a database that stores the identity of a user's role without programming, led by the System wizard. For example, we now need to create a database of access, which stores user names, passwords, password questions, user email and other information, can be achieved through the Web Site Security Configuration tool. The steps are as follows:
1 First Use Visual Studio Express 2005 to establish a Web site, named Dorknozzle, using the vb.net language, and then we click on the asp.net in the website menu in Visual Studio Express Configuration, this will pop up a new page window, where the security option is selected, as shown in the following figure:
2 as you can see from the image above, the security settings options are divided into three parts: users, roles, and rules. Since we are configuring for the first time, select the "To configure security steps by step, use the security Setup Wizard", in the presence of the interface point "next" into the next step.
In the second step, you can choose the type of validation, such as whether your site is posted on the Internet or an intranet, and then click Next to enter next, and the system prompts you to use AspNetAccessProvider to continue with the next step. The system asks if you want to create a role-based application, and if you need to use a role in your application, you must tick that checkbox and continue to the next step without selecting it.
3 At this time, the system will ask us to enter the creation of user information, such as the following image, such as input user name, password, email and other information. After the input, click Next to go to the next step.
4 The system will allow you to make detailed rules about a user, such as allowing a user to drive or refuse to use a role. This article will ignore the need to continue to select next into the next step, at this time, completed the initial configuration of the system.
At this point, AccessMembershipProvider automatically creates a ASPNETDB access data file in the current project data directory, opens it, and finds that the relevant tables are automatically generated, such as aspnet_users tables, Store the user's basic information, aspnet_membership the table, and store the user's encrypted password and other information.
After completing the above steps, we begin to recognize and use the login controls in asp.net 2.0. The controls are in the login Options group of the Toolbox, with a total of 7 login, LoginView, PasswordRecovery, LoginStatus, LoginName, CreateUserWizard, and ChangePassword. , the following describes:
Login control
Drag a login control into the form, you will find the same as the usual landing window, in the control's Properties window, the more important is the Destinationpageurl property, which will specify the user to jump after landing the page, FailureText indicates the text of the prompt after the login failed, Createusertext indicates the text of the prompt that created the new user, passwordrecoverytext the text that prompts the password, Passwordrecoveryurl represents the implementation of the password hint restore function link, in addition can set many of its properties, such as display user name, password text, landing button Passwordrecoveryurl text, etc., are very simple, we can go to try, The landing box after the initial setting of the property is as follows:
LoginName and LoginStatus controls
General login module, when the user after the successful landing, will display the user's current login status, such as "Welcome XXX User Login" prompt, but also will show "LOGOUT (exit)" hint, at this time, we can through the LoginName and LoginStatus control to achieve, On the index.aspx page, add
<form runat= "Server" >
Welcome <asp:loginname id= "Lnuser" runat= "Server"/>, your login was SUCCESSFUL!<BR/><BR/>
<asp:loginstatus id= "Lsuser" runat= "Server"/>
</form>
When the user logs on, the following is displayed:
LoginView Control
Another very useful control is the LoginView. It will display different landing content depending on the user's role. For example, as an administrator after landing, want to see all departments of personnel information, and only want a department of people to see only the Department of Information, LoginView control can do this. In the example of this article, the message that you want the system to display to anonymous visitors is to want them to sign up for users, and for those who have already logged in, you will be welcome to visit them again. The anonymoustemplate template and LoggedInTemplate template for the LoginView control are used at this time. The anonymoustemplate template is used to display the prompt information to anonymous users, the LoggedInTemplate template is used to display the prompt information to the logged in user, the code is as follows:
<asp:loginview id= "Lvdorknozzle" runat= "Server"
<LoggedInTemplate>
Welcome <asp:loginname id= "Lnuser" runat= "Server"/>
</LoggedInTemplate>
<AnonymousTemplate>
Welcome to the Dorknozzle SITE!<BR/>
Please click ' on the ' New User link to register on our site.
</AnonymousTemplate>
</asp:LoginView>
Run the following figure:
CreateUserWizard control
With this control, you can easily guide the user to sign up in a step-by-step manner. Drag it to the design form, as shown in the following figure:
Of course, you can set more of its properties, such as setting HeaderTemplate (header Template), Startnavigationtemplate (start registration template), Finishnavigationtemplate (end registration template), etc., Specific settings can refer to online Help. The control also allows you to send a message to a user after a successful registration, such as:
<asp:createuserwizard id= "CreateUserWizard1" runat= "Server"
<maildefinition
Bodyfilename= "NewUserEmail.txt"
From= "Welcome@dorknozzle.com"
Subject= "Welcome to the Dorknozzle site!" />
</asp:CreateUserWizard>
Sending a message requires you to configure it in the Web.config file, such as:
<configuration>
<system.web>
<authentication mode= "Forms"/>
<smtpmail servername= "Localhost"/>
</system.web>
</configuration>
PasswordRecovery Password Recovery control
The control can implement a simple password prompt recovery function and email the password to the user with the following code:
<asp:passwordrecovery id= "Prforgotpass" runat= "Server" > </asp:PasswordRecovery>
The implementation is shown in the following illustration:
The user enters the user name, then needs to enter the password recovery The answer, the following figure:
And in the control, set the restored password to email to the user
<asp:passwordrecovery id= "Prforgotpass" runat= "Server"
<maildefinition
Bodyfilename= "Forgotpassword.txt"
From= "Helpdesk@dorknozzle.com"
Subject= "Word has it, you forgot your password?" />
</asp:PasswordRecovery>
ChangePassword Change Password control
Changing the password control makes it easy to implement password changes, including a MailDefinition property. If you assign a value to the MailDefinition property, the ChangePassword control automatically sends an e-mail message to the user when the password is successfully changed.
Direct use of the membership API
Sometimes, you need to have a higher level of membership (than the control level provided by the WEB Site Administration tool or Login control). In these cases, you can use the membership API directly.
The membership API is exposed through the membership class. The Membership class contains methods that enable you to create new users, change passwords, search for users who match specific criteria, and so on. Behind the scenes, the Login control uses these methods to interact with the configured membership provider.
Some of the more important methods of the membership class are listed below:
Createuser-enables you to create new users.
deleteuser-enables you to delete existing users.
Findusersbyemail-enables you to retrieve a collection of users that match a particular e-mail address.
Findusersbyname-enables you to retrieve a collection of users that match a particular user name.
Generatepassword-allows you to generate random passwords.
Getallusers-enables you to retrieve all users stored in a membership provider.
Getnumberofusersonline-enables you to return the number of users who are currently accessing the WEB application.
Getuser-enables you to retrieve the membership information associated with the current user, or to enable you to retrieve the membership information associated with a user who has provided a user name.
Getusernamebyemail-enables you to retrieve the user name of a user with a specific e-mail address.
updateuser-enables you to update information for a specific user.
Validateuser-enables you to authenticate users based on the membership provider.
Finally, we use the API to implement a slightly more complex function. In Index.aspx, add a Label control that displays the number of people online, and the code is as follows:
Number of Users Online:
<asp:label id= "Lblnumusersonline" runat= "Server"/>
In the Page-load event, add the following code:
Sub Page_Load (S as Object, E as EventArgs) Handles MyBase.Load
Lblnumusersonline.text = _
Membership.getnumberofusersonline (). ToString ()
End Sub
You can display the number of people online. We also add a GridView that shows the online status of all users, whether certain permissions and related information are allowed.
<asp:gridview id= "gvusers" runat= "Server" autogeneratecolumns= "False"
<Columns>
<asp:boundfield headertext= "Username" datafield= "Username"
<asp:boundfield headertext= "is Online?" Datafield= "Isonline"/>
<asp:boundfield headertext= "is Approved?" Datafield= "isapproved"/>
<asp:boundfield headertext= "Email" datafield= "email"
</Columns>
</asp:GridView>
And bind the GridView:
Gvusers.datasource = Membership.getallusers ();
Gvusers.databind ();
The results of the final operation are shown below:
Summary: This article takes a practical example to introduce the simple usage of the newly added login-series controls in ASP.net 2.0 and the simple use of the newly added membership APIs, and more on the MSDN or official version of the introduction.