Flier's Sky

天空,蓝色的天空,眼睛看不到的东西,眼睛看得到的东西

导航

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

Posted on 2004-12-01 23:57  Flier Lu  阅读(2319)  评论(0编辑  收藏  举报
原文: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
{
  
private WindowsIdentity m_identity;
  
private string[] m_roles = null;
  
private bool m_rolesLoaded = false;

  
public virtual bool IsInRole(string role)
  
{
    
if (role == nullreturn false;

    
if (m_rolesLoaded)
    
{
      m_roles 
= this.m_identity.GetRoles();
      m_rolesLoaded 
= true;
    }


    
foreach(string rolename in m_roles)
    
{
      
if (string.Compare(rolename, role, true, CultureInfo.InvariantCulture) == 0)
      
{
        
return true;
      }

    }

    
return false;
  }

}

    为了线程安全和查找效率,真实实现里还使用了诸如 double-check 模式避免线程死锁,并在角色过多(24个以上)时使用 Hashtable 优化查询性能等等。

    不过对我们来说,这已经足够了。我们可以通过直接调用 WindowsIdentity.GetRoles 方法,获得当前帐号所在的所有角色列表(也就是 Windows 下的组),了解其命名方法或给出调试日志信息。例如:

WindowsIdentity identity = WindowsIdentity.GetCurrent();

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

String[] roleNames 
= (String[])method.Invoke(identity, new object[] {});

foreach(string roleName in roleNames)
{
  Console.WriteLine(roleName);
}

    采用类似的原理,我们还可以直接访问保存 Forms 认证模式的角色列表的 GenericPrincipal 对象的 m_roles 字段,获得当前 Forms 认证用户的角色列表。

    而在 .NET Framework v2.0 中,则引入了安全描述符 SecurityIdentifier 类型的定义。由 WindowsPrincipal.IsInRole 方法把角色名称,通过 NTAccount.Translate 从 NTAccount 类型帐号转换为对应的 SecurityIdentifier 类型安全描述符。

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[0as SecurityIdentifier;

    
return (sid == null? false : IsInRole(sid);
  }

}

    而对用户是否属于某个角色的判断,也由简单的字符串搜索,改为调用系统 CheckTokenMembership 函数完成。因此如果需要获取 Windows 用户所在组列表,就必须通过正规的 Windows 安全相关函数完成。有兴趣的朋友可以参考《Programming WINDOWS Security》一书相关章节。不过 Forms 认证模式的角色列表获取方法仍然有效。