The Labs \ Source Viewer \ SSCLI \ System.Security.Principal \ GenericPrincipal

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //
  16. // GenericPrincipal.cs
  17. //
  18. namespace System.Security.Principal
  19. {
  20.     using System.Runtime.Remoting;
  21.     using System;
  22.     using System.Security.Util;
  23.     using System.Globalization;
  24.    
  25.     [Serializable()]
  26.     [System.Runtime.InteropServices.ComVisible(true)]
  27.     public class GenericPrincipal : IPrincipal
  28.     {
  29.         private IIdentity m_identity;
  30.         private string[] m_roles;
  31.        
  32.         public GenericPrincipal(IIdentity identity, string[] roles)
  33.         {
  34.             if (identity == null)
  35.                 throw new ArgumentNullException("identity");
  36.            
  37.             m_identity = identity;
  38.             if (roles != null) {
  39.                 m_roles = new string[roles.Length];
  40.                 for (int i = 0; i < roles.Length; ++i) {
  41.                     m_roles[i] = roles[i];
  42.                 }
  43.             }
  44.             else {
  45.                 m_roles = null;
  46.             }
  47.         }
  48.        
  49.         public virtual IIdentity Identity {
  50.             get { return m_identity; }
  51.         }
  52.        
  53.         public virtual bool IsInRole(string role)
  54.         {
  55.             if (role == null || m_roles == null)
  56.                 return false;
  57.            
  58.             for (int i = 0; i < m_roles.Length; ++i) {
  59.                 if (m_roles[i] != null && String.Compare(m_roles[i], role, StringComparison.OrdinalIgnoreCase) == 0)
  60.                     return true;
  61.             }
  62.             return false;
  63.         }
  64.     }
  65. }

Developer Fusion