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);
}
}
}