RolePrincipal.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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.IO;
  35. using System.Text;
  36. namespace System.Web.Security {
  37. [Serializable]
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. #if NET_4_0
  40. public
  41. #else
  42. public sealed
  43. #endif
  44. class RolePrincipal : IPrincipal {
  45. IIdentity _identity;
  46. bool _listChanged;
  47. string[] _cachedArray;
  48. HybridDictionary _cachedRoles;
  49. readonly string _providerName;
  50. int _version = 1;
  51. string _cookiePath;
  52. DateTime _issueDate;
  53. DateTime _expireDate;
  54. public RolePrincipal (IIdentity identity)
  55. {
  56. if (identity == null)
  57. throw new ArgumentNullException ("identity");
  58. this._identity = identity;
  59. this._cookiePath = RoleManagerConfig.CookiePath;
  60. this._issueDate = DateTime.Now;
  61. this._expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
  62. }
  63. public RolePrincipal (IIdentity identity, string encryptedTicket)
  64. : this (identity)
  65. {
  66. DecryptTicket (encryptedTicket);
  67. }
  68. public RolePrincipal (string providerName, IIdentity identity)
  69. : this (identity)
  70. {
  71. if (providerName == null)
  72. throw new ArgumentNullException ("providerName");
  73. this._providerName = providerName;
  74. }
  75. public RolePrincipal (string providerName, IIdentity identity, string encryptedTicket)
  76. : this (providerName, identity)
  77. {
  78. DecryptTicket (encryptedTicket);
  79. }
  80. public string [] GetRoles ()
  81. {
  82. if (!_identity.IsAuthenticated)
  83. return new string[0];
  84. if (!IsRoleListCached || Expired) {
  85. _cachedArray = Provider.GetRolesForUser (_identity.Name);
  86. _cachedRoles = new HybridDictionary (true);
  87. foreach (string r in _cachedArray)
  88. _cachedRoles.Add(r, r);
  89. _listChanged = true;
  90. }
  91. return _cachedArray;
  92. }
  93. public bool IsInRole (string role)
  94. {
  95. if (!_identity.IsAuthenticated)
  96. return false;
  97. GetRoles ();
  98. return _cachedRoles [role] != null;
  99. }
  100. public string ToEncryptedTicket ()
  101. {
  102. string roles = string.Join (",", GetRoles ());
  103. string cookiePath = RoleManagerConfig.CookiePath;
  104. int approxTicketLen = roles.Length + cookiePath.Length + 64;
  105. if (_cachedArray.Length > Roles.MaxCachedResults)
  106. return null;
  107. MemoryStream ticket = new MemoryStream (approxTicketLen);
  108. BinaryWriter writer = new BinaryWriter (ticket);
  109. // version
  110. writer.Write (Version);
  111. // issue datetime
  112. DateTime issueDate = DateTime.Now;
  113. writer.Write (issueDate.Ticks);
  114. // expiration datetime
  115. writer.Write (_expireDate.Ticks);
  116. writer.Write (cookiePath);
  117. writer.Write (roles);
  118. CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
  119. byte[] ticket_data = ticket.GetBuffer ();
  120. if (cookieProtection == CookieProtection.All) {
  121. ticket_data = MachineKeySectionUtils.EncryptSign (MachineConfig, ticket_data);
  122. } else if (cookieProtection == CookieProtection.Encryption) {
  123. ticket_data = MachineKeySectionUtils.Encrypt (MachineConfig, ticket_data);
  124. } else if (cookieProtection == CookieProtection.Validation) {
  125. ticket_data = MachineKeySectionUtils.Sign (MachineConfig, ticket_data);
  126. }
  127. return GetBase64FromBytes (ticket_data, 0, ticket_data.Length);
  128. }
  129. void DecryptTicket (string encryptedTicket)
  130. {
  131. if (encryptedTicket == null || encryptedTicket == String.Empty)
  132. throw new ArgumentException ("Invalid encrypted ticket", "encryptedTicket");
  133. byte [] ticketBytes = GetBytesFromBase64 (encryptedTicket);
  134. byte [] decryptedTicketBytes = null;
  135. CookieProtection cookieProtection = RoleManagerConfig.CookieProtection;
  136. if (cookieProtection == CookieProtection.All) {
  137. decryptedTicketBytes = MachineKeySectionUtils.VerifyDecrypt (MachineConfig, ticketBytes);
  138. } else if (cookieProtection == CookieProtection.Encryption) {
  139. decryptedTicketBytes = MachineKeySectionUtils.Decrypt (MachineConfig, ticketBytes);
  140. } else if (cookieProtection == CookieProtection.Validation) {
  141. decryptedTicketBytes = MachineKeySectionUtils.Verify (MachineConfig, ticketBytes);
  142. }
  143. if (decryptedTicketBytes == null)
  144. throw new HttpException ("ticket validation failed");
  145. MemoryStream ticket = new MemoryStream (decryptedTicketBytes);
  146. BinaryReader reader = new BinaryReader (ticket);
  147. // version
  148. _version = reader.ReadInt32 ();
  149. // issued date
  150. _issueDate = new DateTime (reader.ReadInt64 ());
  151. // expire date
  152. _expireDate = new DateTime (reader.ReadInt64 ());
  153. // cookie path
  154. _cookiePath = reader.ReadString ();
  155. // roles
  156. string roles = reader.ReadString ();
  157. if (!Expired) {
  158. InitializeRoles (roles);
  159. //update ticket if less than half of CookieTimeout remaining.
  160. if (Roles.CookieSlidingExpiration){
  161. if (_expireDate-DateTime.Now < TimeSpan.FromTicks (RoleManagerConfig.CookieTimeout.Ticks/2)) {
  162. _issueDate = DateTime.Now;
  163. _expireDate = DateTime.Now.Add (RoleManagerConfig.CookieTimeout);
  164. SetDirty ();
  165. }
  166. }
  167. } else {
  168. // issue a new ticket
  169. _issueDate = DateTime.Now;
  170. _expireDate = _issueDate.Add (RoleManagerConfig.CookieTimeout);
  171. }
  172. }
  173. void InitializeRoles (string decryptedRoles)
  174. {
  175. _cachedArray = decryptedRoles.Split (',');
  176. _cachedRoles = new HybridDictionary (true);
  177. foreach (string r in _cachedArray)
  178. _cachedRoles.Add (r, r);
  179. }
  180. public bool CachedListChanged {
  181. get { return _listChanged; }
  182. }
  183. public string CookiePath {
  184. get { return _cookiePath; }
  185. }
  186. public bool Expired {
  187. get { return ExpireDate < DateTime.Now; }
  188. }
  189. public DateTime ExpireDate {
  190. get { return _expireDate; }
  191. }
  192. public IIdentity Identity {
  193. get { return _identity; }
  194. }
  195. public bool IsRoleListCached {
  196. get { return (_cachedRoles != null) && RoleManagerConfig.CacheRolesInCookie; }
  197. }
  198. public DateTime IssueDate {
  199. get { return _issueDate; }
  200. }
  201. public string ProviderName {
  202. get { return String.IsNullOrEmpty(_providerName) ? Provider.Name : _providerName; }
  203. }
  204. public int Version {
  205. get { return _version; }
  206. }
  207. RoleProvider Provider {
  208. get {
  209. if (String.IsNullOrEmpty (_providerName))
  210. return Roles.Provider;
  211. return Roles.Providers [_providerName];
  212. }
  213. }
  214. public void SetDirty ()
  215. {
  216. _listChanged = true;
  217. _cachedRoles = null;
  218. _cachedArray = null;
  219. }
  220. static string GetBase64FromBytes (byte [] bytes, int offset, int len)
  221. {
  222. return Convert.ToBase64String (bytes, offset, len);
  223. }
  224. static byte [] GetBytesFromBase64 (string base64String)
  225. {
  226. return Convert.FromBase64String (base64String);
  227. }
  228. RoleManagerSection RoleManagerConfig
  229. {
  230. get { return (RoleManagerSection) WebConfigurationManager.GetSection ("system.web/roleManager"); }
  231. }
  232. MachineKeySection MachineConfig
  233. {
  234. get { return (MachineKeySection) WebConfigurationManager.GetSection ("system.web/machineKey"); }
  235. }
  236. }
  237. }