Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选

首页 / 网页编程 / ASP.NET / Asp.net用户管理API的应用

Asp.net用户管理API的应用2010-12-15Asp.net官方标准控件实现用户的管理,虽然简单,但控件封装性很强,开发人员不能明白做了什么样的调用,还用别一方面,标准控件的使用,很大程度上限制了程序的可变性。如果自开发一整套用户管理系统,可行,但又失去了标准用户控件的作用,于是用API来管理用户,成为一个很好的先择,下面我列出主要(不 全部)的用户管理API实例:

1、注册用户

用Membership.CreateUser来创建设新用户,注意密友要包含一个符号,Membership位于System.Web.Security命名空间内。

//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代码

1 <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
2 <asp:TextBox ID="name" runat="server" Width="196px"></asp:TextBox>
3 <asp:Label ID="Label2" runat="server" Text="密码:"></asp:Label>
4 <asp:TextBox ID="password" runat="server" Width="197px"></asp:TextBox>
5 <asp:Label ID="Label3" runat="server" Text="确认密码:"></asp:Label>
6 <asp:TextBox ID="OtherPass" runat="server" Width="196px"></asp:TextBox>
7 <asp:Label ID="Label4" runat="server" Text="电子邮件:"></asp:Label>
8 <asp:TextBox ID="email" runat="server" Width="193px"></asp:TextBox>
9 <asp:Label ID="Label5" runat="server" Text="安全提示问题:"></asp:Label>
10 <asp:TextBox ID="question" runat="server" Width="189px"></asp:TextBox>
11 <asp:Label ID="Label6" runat="server" Text="安全答案:"></asp:Label>
12 <asp:TextBox ID="answer" runat="server" Width="187px"></asp:TextBox>
13 <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="注册" Width="69px" />
14
15

2、用户登录

用户登录用Membershi.ValidateUser来验证用户名和密码。如果通过验证,调用FormsAuthentication.RedirectFromLoginPage导向目标页面(这里以及后面的一些设置都是配合Forms验证展开,都预先在web.config中配置好Forms的验证策略)。

//cs代码,在登录按钮的单击事件注册的方法中

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代码

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