Application of ASP.net user management API

Source: Internet
Author: User

asp.net official standard control to achieve user management, although simple, but the control encapsulation is very strong, developers can not understand what kind of call, but also with the other hand, the use of standard controls, to a large extent, limit the variability of the program. If you develop a set of user management systems that are feasible but lose the role of standard user controls, and then use APIs to manage users as a good choice, I'll list the main (not all) User Management API examples below:

1, Registered users

Use Membership.CreateUser to create new users, pay attention to close friends to include a symbol, membership is located in the System.Web.Security namespace.

Cs

1try
2    {
3      MembershipCreateStatus MCS;
4      Membership.CreateUser(name.Text, password.Text,email .Text ,question .Text,answer .Text ,true , out MCS );
5      Response.Write(MCS.ToString ());
6    }
7    catch(Exception s)
8    {
9      //异常处理代码
10    }
11

ASPX code

1 <asp:label id= "Label1" runat= "server" text= "username:" ></ASP:LABEL>
2 <asp:textbox id= "name" runat= "Server" width= "196px" ></asp:textbox>
3 <asp:label id= "Label2" runat= "server" text= "Password:" >  </asp:label>
4 <asp:textbox id= "password" runat= "server" width= "197px" ></ASP:TEXTBOX>
5 <asp:label id= "Label3" runat= "Server" text= "Confirm password:" ></ASP:LABEL>
6 <asp:textbox id= "Otherpass" runat= "Server" width= "196px" ></asp:textbox>
7 <asp:label id= "Label4" runat= "Server" text= "e-mail:" > </asp:label>
8 <asp:textbox id= "Email" runat= "server" width= "193px" ></ASP:TEXTBOX>
9 < Asp:label id= "Label5" runat= "Server" text= "security hint issue:" ></ASP:LABEL>
<asp:textbox id= "question" runat= "Server" width= "189px" ></asp:textbox>
One <asp:label id= "Label6" runat= "Server" text= "Security Answer:" ></ Asp:label>
<asp:textbox id= "Answer" runat= "Server" WIdth= "187px" ></asp:textbox>
<asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "Register" width= "69px"/>

2, User Login

User logon uses Membershi.validateuser to authenticate user name and password. If you pass validation, Call the FormsAuthentication.RedirectFromLoginPage-oriented target page (this and some of the following settings are expanded in conjunction with forms validation to have the authentication policy for forms in advance configured in Web.config).

CS code, in the Login button's Click event Registration method

1if (Membership.ValidateUser(UserName.Text,Password.Text))
2    {
3      FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
4    }
5    else
6    {
7      Response.Write("登录失败!");
8    }
9
10

ASPX code

1<asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
2 <asp:TextBox ID="UserNmae" runat="server"></asp:TextBox>
3 <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
4 <asp:TextBox ID="Password" runat="server"></asp:TextBox>
5 <asp:Button ID="Login_But" runat="server" onclick="Button1_Click" Text="登录"
6  Width="69px" />
7 <asp:HyperLink ID="FindPass_HL" runat="server" NavigateUrl="~/FindPassword.aspx">忘记密码</asp:HyperLink>
8<asp:HyperLink ID="Reg_HL" runat="server" NavigateUrl="~/register.aspx">注册</asp:HyperLink>
9 
10
11

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.