|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
|
在使用 Forms 模式认证的基于角色的 ASP.NET 系统里面,可以通过 HttpContext.Current.User 属性,来判断当前用户的角色,这里的角色只是在配置文件中定义的一个字符串形式名称而已。 PbqLinux联盟 PbqLinux联盟 但对于 Windows 模式认证的情况,要通过 HttpContext.Current.User 或 Thread.CurrentPrincipal 判断用户角色,就必须考虑到角色命名格式的问题。因为在 Windows 模式认证的情况下,IPrincipal 接口的实现是由 WindowsPrincipal 类型完成的,其角色名称是直接通过 WindowsIdentity 类型的内部方法,从操作系统或 AD 获取并命名的。 PbqLinux联盟 PbqLinux联盟 按 MSDN 里的说法,这里的角色命名方法包括内建角色的 "BUILTIN" 前缀、本机角色的 "MACHINENAME" 前缀和 "DOMAINNAME" 前缀,而且对 FX 1.0 版本还有大小写的限制。 PbqLinux联盟 PbqLinux联盟 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. PbqLinux联盟 PbqLinux联盟 For machine- or domain-specific roles, the role string should be in the form "MACHINENAME\RoleNameHere" and "DOMAINNAME\RoleNameHere". PbqLinux联盟 PbqLinux联盟 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. PbqLinux联盟 PbqLinux联盟 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. PbqLinux联盟 PbqLinux联盟 PbqLinux联盟 选择在什么时候使用什么样的名称,这时就会变成一件头痛的事情,可能需要多次反复的尝试 :S PbqLinux联盟 PbqLinux联盟 好在通过对其实现方法的分析,我们可以获得一个非常简便的了解命名规则的方法,并能够编写一些辅助函数来判断命名的有效性。要了解此方法,我们先来看看 WindowsPrincipal 对象的实现: PbqLinux联盟 PbqLinux联盟 .NET Framework v1.1 在实现上 WindowsPrincipal 对象只是一个角色名称的缓存而已。它保留构造时传入的 WindowsIdentity 对象引用,在需要判断角色时,动态获取其角色列表并缓存在本地。其核心函数 IPrincipal.IsInRole 的实现伪代码如下: PbqLinux联盟 PbqLinux联盟 public class System.Security.Principal.WindowsPrincipal : IPrincipal PbqLinux联盟 { PbqLinux联盟 private WindowsIdentity m_identity; PbqLinux联盟 private string[] m_roles = null; PbqLinux联盟 private bool m_rolesLoaded = false; PbqLinux联盟 PbqLinux联盟 public virtual bool IsInRole(string role) PbqLinux联盟 { PbqLinux联盟 if (role == null) return false; PbqLinux联盟 PbqLinux联盟 if (m_rolesLoaded) PbqLinux联盟 { PbqLinux联盟 m_roles = this.m_identity.GetRoles(); PbqLinux联盟 m_rolesLoaded = true; PbqLinux联盟 } PbqLinux联盟 PbqLinux联盟 foreach(string rolename in m_roles) PbqLinux联盟 { PbqLinux联盟 if (string.Compare(rolename, role, true, CultureInfo.InvariantCulture) == 0) PbqLinux联盟 { PbqLinux联盟 return true; PbqLinux联盟 } PbqLinux联盟 } PbqLinux联盟 return false; PbqLinux联盟 } PbqLinux联盟 } PbqLinux联盟 为了线程安全和查找效率,真实实现里还使用了诸如 double-check 模式避免线程死锁,并在角色过多(24个以上)时使用 Hashtable 优化查询性能等等。 PbqLinux联盟 PbqLinux联盟 不过对我们来说,这已经足够了。我们可以通过直接调用 WindowsIdentity.GetRoles 方法,获得当前帐号所在的所有角色列表(也就是 Windows 下的组),了解其命名方法或给出调试日志信息。例如: PbqLinux联盟 PbqLinux联盟 WindowsIdentity identity = WindowsIdentity.GetCurrent(); PbqLinux联盟 PbqLinux联盟 MethodInfo method = identity.GetType().GetMethod("GetRoles", BindingFlags.Instance | BindingFlags.NonPublic); PbqLinux联盟 PbqLinux联盟 String[] roleNames = (String[])method.Invoke(identity, new object[] {}); PbqLinux联盟 PbqLinux联盟 foreach(string roleName in roleNames) PbqLinux联盟 { PbqLinux联盟 Console.WriteLine(roleName); PbqLinux联盟 } PbqLinux联盟 采用类似的原理,我们还可以直接访问保存 Forms 认证模式的角色列表的 GenericPrincipal 对象的 m_roles 字段,获得当前 Forms 认证用户的角色列表。 PbqLinux联盟 PbqLinux联盟 而在 .NET Framework v2.0 中,则引入了安全描述符 SecurityIdentifier 类型的定义。由 WindowsPrincipal.IsInRole 方法把角色名称,通过 NTAccount.Translate 从 NTAccount 类型帐号转换为对应的 SecurityIdentifier 类型安全描述符。 PbqLinux联盟 PbqLinux联盟 class System.Security.Principal.WindowsPrincipal PbqLinux联盟 { PbqLinux联盟 public virtual bool IsInRole(string role) PbqLinux联盟 { PbqLinux联盟 if ((role == null) || (role.Length == 0)) return false; PbqLinux联盟 PbqLinux联盟 IdentityReferenceCollection collection1 = new IdentityReferenceCollection(1); PbqLinux联盟 PbqLinux联盟 collection1.Add(new NTAccount(role)); PbqLinux联盟 PbqLinux联盟 IdentityReferenceCollection collection2 = NTAccount.Translate(collection1, typeof(SecurityIdentifier), false); PbqLinux联盟 PbqLinux联盟 SecurityIdentifier sid = collection2[0] as SecurityIdentifier; PbqLinux联盟 PbqLinux联盟 return (sid == null) ? false : IsInRole(sid); PbqLinux联盟 } PbqLinux联盟 } PbqLinux联盟 而对用户是否属于某个角色的判断,也由简单的字符串搜索,改为调用系统 CheckTokenMembership 函数完成。因此如果需要获取 Windows 用户所在组列表,就必须通过正规的 Windows 安全相关函数完成。
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|