Asp.net用户管理API的应用(上)(图)

  ASP.NET官方标准控件实现用户的管理,虽然简单,但控件封装型很强,开发人员不能明白做了什么样的调用,还有另一方面,标准控件的使用,很大程度上限制了程序的可变性。如果自开发一套用户管理系统,可行,但又失去了标准控件的作用,于是用API来管理用户,成为一个很好的选择,下面列出主要的用户管理APS实例:

  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 }

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" Text="注册" Width="69px" />

  2.用户登录

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

cs代码

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

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" Text="登录" 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>

  3.找回密码

  CS中的邮件发送方法,关于一些邮件的配置是在web.config中存放,方法中有相关的获取方法

cs代码

1using System;
2
using System.Collections;
3
using System.Configuration;
4
using System.Data;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.HtmlControls;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.WebControls.WebParts;
11
using System.Web.Configuration;
12
using System.Net.Configuration;
13
using System.Net.Mail ;
14
public partial class FindPassword : System.Web.UI.Page
15{
16 protected void Page_Load(object sender, EventArgs e)
17 {
18 if (!IsPostBack)
19 {
20 Wizard1.ActiveStepIndex = 0;
21 }
22 }
23 protected void Wizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
24 {
25 try
26 {
27 Label1.Text = "问题是:" + Membership.GetUser(Quest_TB.Text).PasswordQuestion;
28 }
29 catch (Exception ee)
30 {
31 Response.Write("异常,详细错误:"+ee.Message);
32 }
33 }
34 protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
35 {
36 try
37 {
38 Configuration c = WebConfigurationManager.OpenWebConfiguration(@"~\web.config"); ;
39 NetSectionGroup ns = NetSectionGroup.GetSectionGroup(c);
40 string forms = ns.MailSettings.Smtp.From;
41 string hosts = ns.MailSettings.Smtp.Network.Host;
42 int ports = ns.MailSettings.Smtp.Network.Port;
43 string usernames = ns.MailSettings.Smtp.Network.UserName;
44 string passwords = ns.MailSettings.Smtp.Network.Password;
45 MailAddress from = new MailAddress(forms);
46 MailAddress to = new MailAddress(Membership.GetUser(TextBox1.Text).Email);
47 MailMessage message = new MailMessage(from, to);
48 message.Subject = "密码";
49 string nr = "您好:你的密码为:" + Membership.GetUser(Quest_TB.Text).ResetPassword(Answer_TB.Text);
50 message.Body = nr;
51 SmtpClient client = new SmtpClient(hosts, ports);
52 client.Send(message);
53 }
54 catch (Exception ee)
55 {
56 Response.Write("发送邮箱密码错误!详细信息:"+ee.Message);
57 }
58 }
59}

aspx代码

1<asp:Wizard ID="Wizard1" runat="server" ActiveStepIndex="2"
2 DisplaySideBar="False" Height="103px"
3 onfinishbuttonclick="Wizard1_FinishButtonClick"
4 onnextbutt Width="168px">
5 <WizardSteps>
6 <asp:WizardStep runat="server" title="用户名">
7 请输入用户名:<br />
8 <asp:TextBox ID=" Quest_TB" runat="server" Width="141px"></asp:TextBox>
9 </asp:WizardStep>
10 <asp:WizardStep runat="server" title="问题">
11 <asp:Label ID="Label1" runat="server" Text="问题是:"></asp:Label>
12 <br />
13 <asp:Label ID="Label2" runat="server" Text="问题:"></asp:Label>
14 <br />
15 <asp:TextBox ID="Answer_TB" runat="server" Width="161px"></asp:TextBox>
16 <br />
17 </asp:WizardStep>
18 <asp:WizardStep runat="server" Title="完成">
19 <asp:Label ID="Label3" runat="server" Text="修改密码完成!"></asp:Label>
20 </asp:WizardStep>
21 </WizardSteps>
22</asp:Wizard>
23//web.config中的配置
24位于configuration标签中
25<system.net>
26 <mailSettings>
27 <smtp from="axzxs2001@163.com">
28 <network host="smtp.163.com" password="*********" userName="axzxs2001" />
29 </smtp>
30 </mailSettings>
31 </system.net>
还有一些用户管理API在下一章叙述。
posted @ 2011-03-23 10:04  Alex.Net  阅读(194)  评论(0编辑  收藏  举报