1. 創建Login.aspx,News文件夾,BBS文件夾,在兩個文件夾內分別有一個Default.aspx頁面
2. 修改Web.config文件,如下:
<authentication mode="Forms">
<forms name=".ASPXFORMSDEMO" loginUrl="Login.aspx" protection="All" path="/" timeout="30">
<credentials passwordFormat="Clear">
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
<allow users="A,B" />
</authorization>
<location path="BBS">
<system.web>
<authorization>
<deny users="?,A"/>
<allow users="B"/>
</authorization>
</system.web>
</location>
<location path="News">
<system.web>
<authorization>
<deny users="?,B"/>
<allow users="A"/>
</authorization>
</system.web>
</location>
注:<location path="BBS">代表對BBS文件夾進行權限設置。允許B組用戶,不允許匿名用戶和A組用戶登錄。
- 創建Login.aspx
string group;
protected void btnLogin_Click(object sender, EventArgs e)
{
//從Web.config中取出連接字符串
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection con = new SqlConnection(conStr);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from users where uname='" + txtName.Text + "'";
cmd.Connection = con;
SqlDataReader dr=cmd.ExecuteReader();
if (dr.Read())
{
if (dr[1].ToString().Equals(txtPwd.Text))
{
//group是該用戶所在分組
group = dr[2].ToString();
//將該用戶的分組保存進Cookie中
FormsAuthentication.RedirectFromLoginPage(group, true);
}
}
}