----------region.aspx前台-------------
<body>
<form id="form1" runat="server">
<div>
<table style="width:50%;">
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
用户密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
邮箱地址:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="注册" onclick="Button1_Click" /></td>
<td>
</td>
</tr>
</table>
</div>
</form>
</body>
--------region.aspx.cs后台-----------------
public void sendMail(string email, string activeCode,int id)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("邮箱详细账号");
msg.To.Add(email);
msg.Subject = "请激活注册";
StringBuilder contentBuilder = new StringBuilder();
contentBuilder.Append("请单击以下连接完成激活!");
contentBuilder.Append("<a href='http://localhost:5566/CheckActiveCode.aspx?activecode=" + activeCode + "&id="+id+"'>激活</a>");
msg.Body = contentBuilder.ToString(); ;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.126.com";
client.Port = 25;
NetworkCredential credetial = new NetworkCredential();
credetial.UserName = "@之前的部分";
credetial.Password = "邮箱密码";
client.Credentials = credetial;
client.Send(msg);
}
protected void Button1_Click(object sender, EventArgs e)
{
string userName = this.TextBox1.Text;
string password = this.TextBox2.Text;
string email = this.TextBox3.Text;
string activeCode = Guid.NewGuid().ToString().Substring(0, 8);
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
int number;
using (SqlConnection con = new SqlConnection(conStr))
{
string sql = "insert into T_Users (UserName,Password,Email,Active,ActiveCode) values(@username,@password,@email,@active,@activecode) select @@identity";
SqlParameter[] prams = new SqlParameter[] {
new SqlParameter("@username",userName),
new SqlParameter("@password",password),
new SqlParameter("@email",email),
new SqlParameter("@active",false),
new SqlParameter("@activecode",activeCode)
};
using (SqlCommand cmd = new SqlCommand(sql, con))
{
con.Open();
cmd.Parameters.AddRange(prams);
number = Convert.ToInt32(cmd.ExecuteScalar());
}
}
if (number > 0)
{
sendMail(email, activeCode,number);
Response.Redirect("regionMessage.aspx");
}
else
--------Web.config--------------
<!--基于Windows的身份认证-->
<configuration>
<system.web>
<authentication mode="Windows"></authentication>
<authorization>
<!--除了匿名之外别人都可以-->
<deny users="?"/>
<allow users="*"/>
<!-- 授权,除了Test这个匿名,别人都可以-->
<deny users="?"/>
<deny users="Test"/>
<allow users="*"/>
</authorization>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
<!--基于Froms的身份认证-->
<configuration>
<system.web>
<authentication mode="Forms">
<!--loginUrl登录时的那个页面(所有的用户都从登录这个页面进入网站,别的地方不让进)
给Cookie加密protection -->
<forms loginUrl="login.aspx" timeout="2880" name="aaa" protection="Encryption"/>
</authentication>
<!--授权-->
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<!--在images文件夹下的图片是可以被访问的-->
<location path="images">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
<!--在subPages文件夹下的子页是可以被访问的-->
<location path="subpages">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
</configuration>
----------registor.aspx-----------
<body>
<form id="form1" runat="server">
<div>
注册页……
</div>
</form>
</body>
-----------default1.aspx------------
<body>
<form id="form1" runat="server">
<div>
default1页………………
</div>
</form>
</body>
---------------Login.aspx--------------------
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" Xonclick="Button1_Click" />
<br />
<br />
<img src=/blog_article/"images/1.gif />
<br />
<asp:Login ID="LoginControl" runat="server">
</asp:Login>
</div>
</form>
</body>
--------------Login.aspx.cs-----------------------
protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text == "admin" && this.TextBox2.Text == "123")
{
//from授权,
FormsAuthentication.SetAuthCookie(this.TextBox1.Text, false);
}
}
-------------WebService1.asmx.cs-----------------
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public int Add(int a, int b)
{
return a + b;
}
[WebMethod]
public DataSet GetStudent()
{
string constr = ConfigurationManager.ConnectionStrings["studentConstr"].ConnectionString;
string sql = "select * from student";
SqlDataAdapter adapter = new SqlDataAdapter(sql, constr);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
}
==========================Web.config==========================
<configuration>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<connectionStrings>
<add name="studentConstr" connectionString="data source=.;initial catalog=student;user
id=sa;password=111111;"/>
</connectionStrings>
</configuration>
-------WebAddStudent.aspx前台---------------
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
+
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Xonclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
-------WebAddStudent.aspx.cs后台------------
protected void Button1_Click(object sender, EventArgs e)
{
localhost.WebService1 ws=new localhost.WebService1();
this.TextBox3.Text= ws.Add(Convert.ToInt32(this.TextBox1.Text),Convert.ToInt32
(this.TextBox2.Text)).ToString();
this.GridView1.DataSource = ws.GetStudent();
this.GridView1.DataBind();
}
==========================Web.config==========================
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup,
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="userWebService.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<applicationSettings>
<userWebService.Properties.Settings>
<setting name="userWebService_localhost_WebService1" serializeAs="String">
<value>http://localhost:1095/WebService1.asmx</value>
</setting>
</userWebService.Properties.Settings>
</applicationSettings>
</configuration>