您的位置:首页 > 其它

[TIPS] 正确判断当前用户角色

2004-12-01 23:57 323 查看
原文:http://www.blogcn.com/User8/flier_lu/blog/5043440.html

在使用 Forms 模式认证的基于角色的 ASP.NET 系统里面,可以通过 HttpContext.Current.User 属性,来判断当前用户的角色,这里的角色只是在配置文件中定义的一个字符串形式名称而已。

但对于 Windows 模式认证的情况,要通过 HttpContext.Current.User 或 Thread.CurrentPrincipal 判断用户角色,就必须考虑到角色命名格式的问题。因为在 Windows 模式认证的情况下,IPrincipal 接口的实现是由 WindowsPrincipal 类型完成的,其角色名称是直接通过 WindowsIdentity 类型的内部方法,从操作系统或 AD 获取并命名的。

按 MSDN 里的说法,这里的角色命名方法包括内建角色的 "BUILTIN" 前缀、本机角色的 "MACHINENAME" 前缀和 "DOMAINNAME" 前缀,而且对 FX 1.0 版本还有大小写的限制。


For built-in roles, the role string should be in the form "BUILTIN\RoleNameHere". For example, to test for membership in the Windows administrator role, the string representing the role should be "BUILTIN\Administrators". Note that the backslash might need to be escaped.

For machine- or domain-specific roles, the role string should be in the form "MACHINENAME\RoleNameHere" and "DOMAINNAME\RoleNameHere".

Note In .NET Framework version 1.0 the role parameter is case-sensitive. In .NET Framework version 1.1 and later the role parameter is case-insensitive.

Tip When testing for newly created role information, such as a new user or a new group, it is important to log out and log in to force the propagation of role information within the domain. Not doing so can cause the IsInRole test to return false.

选择在什么时候使用什么样的名称,这时就会变成一件头痛的事情,可能需要多次反复的尝试 :S

好在通过对其实现方法的分析,我们可以获得一个非常简便的了解命名规则的方法,并能够编写一些辅助函数来判断命名的有效性。要了解此方法,我们先来看看 WindowsPrincipal 对象的实现:

.NET Framework v1.1 在实现上 WindowsPrincipal 对象只是一个角色名称的缓存而已。它保留构造时传入的 WindowsIdentity 对象引用,在需要判断角色时,动态获取其角色列表并缓存在本地。其核心函数 IPrincipal.IsInRole 的实现伪代码如下:

public class System.Security.Principal.WindowsPrincipal : IPrincipal
WindowsIdentity identity = WindowsIdentity.GetCurrent();

MethodInfo method = identity.GetType().GetMethod("GetRoles", BindingFlags.Instance | BindingFlags.NonPublic);

foreach(string roleName in roleNames)
class System.Security.Principal.WindowsPrincipal
public virtual bool IsInRole(string role)
if ((role == null) || (role.Length == 0)) return false;

IdentityReferenceCollection collection1 = new IdentityReferenceCollection(1);

collection1.Add(new NTAccount(role));

IdentityReferenceCollection collection2 = NTAccount.Translate(collection1, typeof(SecurityIdentifier), false);

SecurityIdentifier sid = collection2[0] as SecurityIdentifier;

return (sid == null) ? false : IsInRole(sid);
}
}
而对用户是否属于某个角色的判断,也由简单的字符串搜索,改为调用系统 CheckTokenMembership 函数完成。因此如果需要获取 Windows 用户所在组列表,就必须通过正规的 Windows 安全相关函数完成。有兴趣的朋友可以参考《Programming WINDOWS Security》一书相关章节。不过 Forms 认证模式的角色列表获取方法仍然有效。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: