今天来讲一下如何使用Roles类进行角色管理,我把角色管理的常用功能全部写到一个页面里,先把这个页的全部代码贴出来,然后再做讲解.
Web.config里的设置:
<
roleManager
enabled
="true"
cacheRolesInCookie
="true"
defaultProvider
="AspNetSqlRoleProvider"
cookieName
=".ASPXROLES"
cookiePath
="/"
cookieTimeout
="20"
cookieSlidingExpiration
="true"
createPersistentCookie
="true"
cookieProtection
="All"
/>
第一行设置允许使用角色管理;第二行是把验证后角色数据放在COOKIE中;第三行和第四行第五行一般情况下不改除非你有多个应用程序在使用角色;第六行设定角色的过期时间,一般和Forms里的timeout设成一样,如果比timeout设的时间长是没有意义的;第七行设定过期时间机制为可调;第八行设定Cookie保护模式;
Roles.aspx
<%
...
@ Page Language="C#" AutoEventWireup="true" CodeFile="Roles.aspx.cs" Inherits="Roles1"
%>

<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>

<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
无标题页
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
table
border
="1"
width
="800"
>
<
tr
>
<
td
style
="width: 303px"
>
现有角色:
</
td
>
<
td
style
="width: 880px"
>
现有用户:
</
td
>
<
td
style
="width: 1248px"
>
<
asp:Label
ID
="Label1"
runat
="server"
></
asp:Label
>
用户所属角色:
</
td
>
<
td
style
="width: 794px"
>
<
asp:Label
ID
="Label2"
runat
="server"
></
asp:Label
>
角色包含的用户:
</
td
>
</
tr
>
<
tr
>
<
td
style
="width: 303px; height: 21px"
>
<
asp:ListBox
ID
="ListBox1"
runat
="server"
>
<
asp:ListItem
>
没有角色
</
asp:ListItem
>
</
asp:ListBox
></
td
>
<
td
style
="width: 880px; height: 21px"
>
<
asp:ListBox
ID
="ListBox2"
runat
="server"
>
<
asp:ListItem
>
没有用户
</
asp:ListItem
>
</
asp:ListBox
></
td
>
<
td
style
="width: 1248px; height: 21px"
>
<
asp:ListBox
ID
="ListBox3"
runat
="server"
SelectionMode
="Multiple"
>
<
asp:ListItem
>
没有进行选择
</
asp:ListItem
>
</
asp:ListBox
></
td
>
<
td
style
="width: 794px; height: 21px"
>
<
asp:BulletedList
ID
="BulletedList1"
runat
="server"
>
</
asp:BulletedList
>
</
td
>
</
tr
>
<
tr
>
<
td
style
="width: 303px"
>
<
asp:Button
ID
="Button6"
runat
="server"
OnClick
="Button6_Click"
Text
="查看该角色包含的用户"
/></
td
>
<
td
style
="width: 880px"
>
<
asp:Button
ID
="Button3"
runat
="server"
OnClick
="Button3_Click"
Text
="查看该用户角色"
/></
td
>
<
td
style
="width: 1248px"
>
<
asp:Button
ID
="Button2"
runat
="server"
Text
="为用户添加角色"
OnClick
="Button2_Click"
/>
<
asp:Button
ID
="Button4"
runat
="server"
Text
="删除用户的角色"
OnClick
="Button4_Click"
/></
td
>
<
td
style
="width: 794px"
>
</
td
>
</
tr
>
</
table
>
<
hr
/>
</
div
>
<
asp:TextBox
ID
="TextBox1"
runat
="server"
></
asp:TextBox
><
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="添加角色"
/><
br
/>
<
asp:TextBox
ID
="TextBox3"
runat
="server"
></
asp:TextBox
><
asp:Button
ID
="Button5"
runat
="server"
OnClick
="Button5_Click"
Text
="删除角色"
/>
</
form
>
</
body
>
</
html
>
Roles.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;

public
partial
class
Roles1 : System.Web.UI.Page

...
{
protected void Page_Load(object sender, EventArgs e)

...{
if (!IsPostBack)

...{
ListBox1.DataSource = Roles.GetAllRoles();//将现有的所有角色绑定到ListBox
ListBox1.DataBind();
ListBox2.DataSource = Membership.GetAllUsers();//将现有的所有用户绑定到ListBox
ListBox2.DataBind();
}
}

//添加角色按钮
protected void Button1_Click(object sender, EventArgs e)

...{
try

...{
Roles.CreateRole(TextBox1.Text);//创建新角色
Response.Write(TextBox1.Text + " 角色创建成功!");
ListBox1.DataSource = Roles.GetAllRoles();//重新绑定角色显示
ListBox1.DataBind();
}
catch (Exception ex)

...{
Response.Write(ex.Message);//如果出错,输出错误信息
}
}

//为用户添加角色按钮
protected void Button2_Click(object sender, EventArgs e)

...{
try

...{
Roles.AddUserToRole(ListBox2.SelectedValue, ListBox1.SelectedValue);//把用户添加到角色里
Response.Write(ListBox2.SelectedValue + " 已经添加到 " + ListBox1.SelectedValue + " 角色中!");

ListBox3.DataSource = Roles.GetRolesForUser(ListBox2.SelectedValue);//把该用户所属的角色绑定给ListBox
ListBox3.DataBind();
}
catch (Exception ex)

...{
Response.Write(ex.Message);
}
}

//查看该用户角色按钮
protected void Button3_Click(object sender, EventArgs e)

...{
if (ListBox2.SelectedIndex >= 0)//判断是否选择了用户

...{
Label1.Text = ListBox2.SelectedValue;
ListBox3.DataSource = Roles.GetRolesForUser(ListBox2.SelectedValue);//把该用户所属的角色绑定给ListBox
ListBox3.DataBind();
}
else

...{
ListBox3.Items[0].Text = "请先选择用户!";
}
}

//删除用户的角色按钮
protected void Button4_Click(object sender, EventArgs e)

...{
if (ListBox2.SelectedItem != null && ListBox3.SelectedItem != null)

...{
int[] iSelected = ListBox3.GetSelectedIndices();//用已选择的角色索引值建立一个数组
string[] sRoles = new string[iSelected.Length];//建立一个数组存放多个角色数据

for (int i = 0; i < iSelected.Length; i++)

...{
sRoles[i] = ListBox3.Items[iSelected[i]].Value;//给角色数组赋值
}

try

...{
Roles.RemoveUserFromRoles(ListBox2.SelectedValue, sRoles);//删除用户的一个或多个角色
Response.Write("用户" + ListBox2.SelectedValue + "的所选角色已删除!");

ListBox3.DataSource = Roles.GetRolesForUser(ListBox2.SelectedValue);//重新绑定一次用户的角色
ListBox3.DataBind();
}
catch (Exception ex)

...{
Response.Write(ex.Message);
}
}
else

...{
Response.Write("请先选择一个用户和一个角色!");
}
}

//删除角色按钮
protected void Button5_Click(object sender, EventArgs e)

...{
try

...{
Roles.RemoveUsersFromRole(Roles.GetUsersInRole(TextBox3.Text), TextBox3.Text);
Roles.DeleteRole(TextBox3.Text);
Response.Write(TextBox3.Text + " 角色已经删除!");

ListBox1.DataSource = Roles.GetAllRoles();//重新绑定现有的所有角色
ListBox1.DataBind();
}
catch (Exception ex)

...{
Response.Write(ex.Message);
}
}

//查看该角色包含用户按钮
protected void Button6_Click(object sender, EventArgs e)

...{
if (ListBox1.SelectedIndex >= 0)//判断是否选择了角色

...{
Label2.Text = ListBox1.SelectedValue;
BulletedList1.DataSource = Roles.GetUsersInRole(ListBox1.SelectedValue);//把该角色包含的用户绑定显示
BulletedList1.DataBind();
}
else

...{
Response.Write("请先选择角色!");
}
}
}
是不是一看就头晕?我以前也是这样一看到长长的代码就烦,不要着急,这些代码都很简单一点也不高深,只是因为懒的多做几个页,所以就把很多功能都合到了这一个页里,其实如果每个功能都分开的话一点也不多,这个页里都实现了如下几个功能:
1、显示所有的角色
2、显示所有的用户
3、显示一个用户所属的角色
4、显示一个角色所包含的用户
5、创建一个新角色
6、删除一个角色
7、给一个用户添加角色
8、删除一个用户的角色
因为今天的时间原因,我就不详细说明每一个功能的实现方法了,代码里我都做了注释,有点基础的朋友都能看懂,明天找个空我会再详细讲述每个功能,要下班了88