RolePrincipal.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // System.Web.Security.RolePrincipal
  3. //
  4. // Authors:
  5. // Ben Maurer ([email protected])
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // (C) 2003 Ben Maurer
  9. // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections.Specialized;
  31. using System.Security.Permissions;
  32. using System.Security.Principal;
  33. using System.Web.Configuration;
  34. using System.Web.Util;
  35. using System.IO;
  36. using System.Text;
  37. namespace System.Web.Security {
  38. [Serializable]
  39. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. #if NET_4_0
  41. public
  42. #else
  43. public sealed
  44. #endif
  45. class RolePrincipal : IPrincipal {
  46. IIdentity _identity;
  47. bool _listChanged;
  48. string[] _cachedArray;
  49. HybridDictionary _cachedRoles;
  50. readonly string _providerName;
  51. int _version = 1;
  52. string _cookiePath;
  53. DateTime _issueDate;
  54. DateTime _expireDate;
  55. public RolePrincipal (IIdentity identity)
  56. {
  57. if (identity == null)
  58. throw new ArgumentNullException ("identity");
  59. this._identity = identity;
  60. this._cookiePath = RoleManagerConfig.CookiePath;
  61. this._issueDate = DateTime.Now;
  62. this._expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
  63. }
  64. public RolePrincipal (IIdentity identity, string encryptedTicket)
  65. : this (identity)
  66. {
  67. DecryptTicket (encryptedTicket);
  68. }
  69. public RolePrincipal (string providerName, IIdentity identity)
  70. : this (identity)
  71. {
  72. if (providerName == null)
  73. throw new ArgumentNullException ("providerName");
  74. this._providerName = providerName;
  75. }
  76. public RolePrincipal (string providerName, IIdentity identity, string encryptedTicket)
  77. : this (providerName, identity)
  78. {
  79. DecryptTicket (encryptedTicket);
  80. }
  81. public string [] GetRoles ()
  82. {
  83. if (!_identity.IsAuthenticated)
  84. return new string[0];
  85. if (!IsRoleListCached || Expired) {
  86. _cachedArray = Provider.GetRolesForUser (_identity.Name);
  87. _cachedRoles = new HybridDictionary (true);
  88. foreach (string r in _cachedArray)
  89. _cachedRoles.Add(r, r);
  90. _listChanged = true;
  91. }
  92. return _cachedArray;
  93. }
  94. public bool IsInRole (string role)
  95. {
  96. if (!_identity.IsAuthenticated)
  97. return false;
  98. GetRoles ();
  99. return _cachedRoles [role] != null;
  100. }
  101. public string ToEncryptedTicket ()
  102. {
  103. string roles = string.Join (",", GetRoles ());
  104. string cookiePath = RoleManagerConfig.CookiePath;
  105. int approxTicketLen = roles.Length + cookiePath.Length + 64;
  106. if (_cachedArray.Length > Roles.MaxCachedResults)
  107. return null;
  108. MemoryStream ticket = new MemoryStream (approxTicketLen);
  109. BinaryWriter writer = new BinaryWriter (ticket);
  110. // version
  111. writer.Write (Version);
  112. // issue datetime
  113. DateTime issueDate = DateTime.Now;
  114. writer.Write (issueDate.Ticks);
  115. // expiration datetime
  116. writer.Write (_expireDate.Ticks);
  117. writer.Write (cookiePath);
  118. writer.Write (roles);
  119. CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
  120. byte[] ticket_data = ticket.GetBuffer ();
  121. if (cookieProtection == CookieProtection.All) {
  122. ticket_data = MachineKeySectionUtils.EncryptSign (MachineConfig, ticket_data);
  123. } else if (cookieProtection == CookieProtection.Encryption) {
  124. ticket_data = MachineKeySectionUtils.Encrypt (MachineConfig, ticket_data);
  125. } else if (cookieProtection == CookieProtection.Validation) {
  126. ticket_data = MachineKeySectionUtils.Sign (MachineConfig, ticket_data);
  127. }
  128. return GetBase64FromBytes (ticket_data, 0, ticket_data.Length);
  129. }
  130. void DecryptTicket (string encryptedTicket)
  131. {
  132. if (encryptedTicket == null || encryptedTicket == String.Empty)
  133. throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
  134. byte [] ticketBytes = GetBytesFromBase64 (encryptedTicket);
  135. byte [] decryptedTicketBytes = null;
  136. CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
  137. if (cookieProtection == CookieProtection.All) {
  138. decryptedTicketBytes = MachineKeySectionUtils.VerifyDecrypt (MachineConfig, ticketBytes);
  139. } else if (cookieProtection == CookieProtection.Encryption) {
  140. decryptedTicketBytes = MachineKeySectionUtils.Decrypt (MachineConfig, ticketBytes);
  141. } else if (cookieProtection == CookieProtection.Validation) {
  142. decryptedTicketBytes = MachineKeySectionUtils.Verify (MachineConfig, ticketBytes);
  143. }
  144. if (decryptedTicketBytes == null)
  145. throw new HttpException ("ticket validation failed");
  146. MemoryStream ticket = new MemoryStream (decryptedTicketBytes);
  147. BinaryReader reader = new BinaryReader (ticket);
  148. // version
  149. _version = reader.ReadInt32 ();
  150. // issued date
  151. _issueDate = new DateTime (reader.ReadInt64 ());
  152. // expire date
  153. _expireDate = new DateTime (reader.ReadInt64 ());
  154. // cookie path
  155. _cookiePath = reader.ReadString ();
  156. // roles
  157. string roles = reader.ReadString ();
  158. if (!Expired) {
  159. InitializeRoles (roles);
  160. //update ticket if less than half of CookieTimeout remaining.
  161. if (Roles.CookieSlidingExpiration){
  162. if (_expireDate-DateTime.Now < TimeSpan.FromTicks (RoleManagerConfig.CookieTimeout.Ticks/2)) {
  163. _issueDate = DateTime.Now;
  164. _expireDate = DateTime.Now.Add (RoleManagerConfig.CookieTimeout);
  165. SetDirty ();
  166. }
  167. }
  168. } else {
  169. // issue a new ticket
  170. _issueDate = DateTime.Now;
  171. _expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
  172. }
  173. }
  174. void InitializeRoles (string decryptedRoles)
  175. {
  176. _cachedArray = decryptedRoles.Split (',');
  177. _cachedRoles = new HybridDictionary (true);
  178. foreach (string r in _cachedArray)
  179. _cachedRoles.Add (r, r);
  180. }
  181. public bool CachedListChanged {
  182. get { return _listChanged; }
  183. }
  184. public string CookiePath {
  185. get { return _cookiePath; }
  186. }
  187. public bool Expired {
  188. get { return ExpireDate < DateTime.Now; }
  189. }
  190. public DateTime ExpireDate {
  191. get { return _expireDate; }
  192. }
  193. public IIdentity Identity {
  194. get { return _identity; }
  195. }
  196. public bool IsRoleListCached {
  197. get { return (_cachedRoles != null) && RoleManagerConfig.CacheRolesInCookie; }
  198. }
  199. public DateTime IssueDate {
  200. get { return _issueDate; }
  201. }
  202. public string ProviderName {
  203. get { return String.IsNullOrEmpty(_providerName) ? Provider.Name : _providerName; }
  204. }
  205. public int Version {
  206. get { return _version; }
  207. }
  208. RoleProvider Provider {
  209. get {
  210. if (String.IsNullOrEmpty (_providerName))
  211. return Roles.Provider;
  212. return Roles.Providers [_providerName];
  213. }
  214. }
  215. public void SetDirty ()
  216. {
  217. _listChanged = true;
  218. _cachedRoles = null;
  219. _cachedArray = null;
  220. }
  221. static string GetBase64FromBytes (byte [] bytes, int offset, int len)
  222. {
  223. return Convert.ToBase64String (bytes, offset, len);
  224. }
  225. static byte [] GetBytesFromBase64 (string base64String)
  226. {
  227. return Convert.FromBase64String (base64String);
  228. }
  229. RoleManagerSection RoleManagerConfig
  230. {
  231. get { return (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager"); }
  232. }
  233. MachineKeySection MachineConfig
  234. {
  235. get { return (MachineKeySection) WebConfigurationManager.GetSection ("system.web/machineKey"); }
  236. }
  237. }
  238. }