Author: krasnov
Date: 2007-02-19 07:41:56 -0500 (Mon, 19 Feb 2007)
New Revision: 73110
Modified:
trunk/mcs/class/System.Web/System.Web.Security/ChangeLog
trunk/mcs/class/System.Web/System.Web.Security/RoleManagerModule.cs
trunk/mcs/class/System.Web/System.Web.Security/RolePrincipal.cs
trunk/mcs/class/System.Web/System.Web.Security/Roles.cs
Log:
* RoleManagerModule.cs: fixed role chaching in OnEndRequest and
OnPostAuthenticateRequest
* RolePrincipal.cs: implemented ToEncryptedTicket, implemented caching role
functionality
* Roles.cs: implemented DeleteCookie, fixed CookieProtectionValue to get it's
value from configuration
Modified: trunk/mcs/class/System.Web/System.Web.Security/ChangeLog
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/ChangeLog 2007-02-19
12:41:26 UTC (rev 73109)
+++ trunk/mcs/class/System.Web/System.Web.Security/ChangeLog 2007-02-19
12:41:56 UTC (rev 73110)
@@ -1,3 +1,12 @@
+2007-02-19 Vladimir Krasnov <[EMAIL PROTECTED]>
+
+ * RoleManagerModule.cs: fixed role chaching in OnEndRequest and
+ OnPostAuthenticateRequest
+ * RolePrincipal.cs: implemented ToEncryptedTicket, implemented caching
+ role functionality
+ * Roles.cs: implemented DeleteCookie, fixed CookieProtectionValue to
+ get it's value from configuration
+
2007-02-08 Marek Habersack <[EMAIL PROTECTED]>
* FormsAuthenticationModule.cs: Expire auth tickets properly.
Modified: trunk/mcs/class/System.Web/System.Web.Security/RoleManagerModule.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/RoleManagerModule.cs
2007-02-19 12:41:26 UTC (rev 73109)
+++ trunk/mcs/class/System.Web/System.Web.Security/RoleManagerModule.cs
2007-02-19 12:41:56 UTC (rev 73110)
@@ -46,7 +46,14 @@
void ClearCookie (HttpApplication app, string cookieName)
{
- new HttpCookie (cookieName, "", "", DateTime.MinValue);
+ RoleManagerSection config = (RoleManagerSection)
WebConfigurationManager.GetSection ("system.web/roleManager");
+ HttpCookie clearCookie = new HttpCookie
(config.CookieName, "");
+
+ clearCookie.Path = config.CookiePath;
+ clearCookie.Expires = DateTime.MinValue;
+ clearCookie.Domain = config.Domain;
+ clearCookie.Secure = config.CookieRequireSSL;
+ app.Response.SetCookie (clearCookie);
}
void OnPostAuthenticateRequest (object sender, EventArgs args)
@@ -58,10 +65,6 @@
if (!config.Enabled)
return;
- /* not supported yet */
- if (config.CacheRolesInCookie)
- throw new NotSupportedException ("Caching roles
in cookie is not supported");
-
/* allow the user to populate the Role */
if (GetRoles != null) {
RoleManagerEventArgs role_args = new
RoleManagerEventArgs (app.Context);
@@ -79,7 +82,7 @@
IIdentity currentIdentity = app.Context.User.Identity;
if (app.Request.IsAuthenticated) {
if (cookie != null) {
- if (config.CacheRolesInCookie)
+ if (!config.CacheRolesInCookie)
cookie = null;
else if (config.CookieRequireSSL &&
!app.Request.IsSecureConnection) {
cookie = null;
@@ -141,15 +144,15 @@
return;
}
- HttpCookie cookie = new HttpCookie (config.CookieName,
- ticket,
- config.CookiePath,
- DateTime.Now +
config.CookieTimeout);
+ HttpCookie cookie = new HttpCookie (config.CookieName,
ticket);
- cookie.Domain = config.Domain;
cookie.HttpOnly = true;
+ if (!string.IsNullOrEmpty (config.Domain))
+ cookie.Domain = config.Domain;
if (config.CookieRequireSSL)
cookie.Secure = true;
+ if (config.CookiePath.Length > 1) // more than '/'
+ cookie.Path = config.CookiePath;
app.Response.SetCookie (cookie);
}
Modified: trunk/mcs/class/System.Web/System.Web.Security/RolePrincipal.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/RolePrincipal.cs
2007-02-19 12:41:26 UTC (rev 73109)
+++ trunk/mcs/class/System.Web/System.Web.Security/RolePrincipal.cs
2007-02-19 12:41:56 UTC (rev 73110)
@@ -31,8 +31,12 @@
#if NET_2_0
using System.Collections.Specialized;
+using System.Security.Cryptography;
using System.Security.Permissions;
using System.Security.Principal;
+using System.Web.Configuration;
+using System.IO;
+using System.Text;
namespace System.Web.Security {
@@ -40,24 +44,33 @@
[AspNetHostingPermission (SecurityAction.LinkDemand, Level =
AspNetHostingPermissionLevel.Minimal)]
public sealed class RolePrincipal : IPrincipal {
- private IIdentity identity;
- private bool listChanged;
- string[] cachedArray;
- private HybridDictionary cachedRoles;
- readonly string providerName;
+ private IIdentity _identity;
+ private bool _listChanged;
+ private string[] _cachedArray;
+ private HybridDictionary _cachedRoles;
+ readonly string _providerName;
+ private int _version = 1;
+ private string _cookiePath;
+ private DateTime _issueDate;
+ private DateTime _exprireDate;
+
+
public RolePrincipal (IIdentity identity)
{
if (identity == null)
throw new ArgumentNullException ("identity");
- this.identity = identity;
+
+ this._identity = identity;
+ this._cookiePath = RoleManagerConfig.CookiePath;
+ this._issueDate = DateTime.Now;
+ this._exprireDate = _issueDate.Add
(RoleManagerConfig.CookieTimeout);
}
- [MonoTODO ("Not implemented")]
public RolePrincipal (IIdentity identity, string
encryptedTicket)
: this (identity)
{
- throw new NotImplementedException ();
+ DecryptTicket (encryptedTicket);
}
public RolePrincipal (string providerName, IIdentity identity)
@@ -66,108 +79,256 @@
if (providerName == null)
throw new ArgumentNullException
("providerName");
- this.providerName = providerName;
+ this._providerName = providerName;
}
- [MonoTODO ("Not implemented")]
public RolePrincipal (string providerName, IIdentity identity,
string encryptedTicket)
- : this (identity)
+ : this (providerName, identity)
{
- if (providerName == null)
- throw new ArgumentNullException
("providerName");
-
- this.providerName = providerName;
-
- throw new NotImplementedException ();
+ DecryptTicket (encryptedTicket);
}
public string [] GetRoles ()
{
- if (!identity.IsAuthenticated)
+ if (!_identity.IsAuthenticated)
return new string[0];
- if (cachedRoles == null) {
- cachedArray = Provider.GetRolesForUser
(identity.Name);
- cachedRoles = new HybridDictionary (true);
+ if (!IsRoleListCached && !Expired) {
+ _cachedArray = Provider.GetRolesForUser
(_identity.Name);
+ _cachedRoles = new HybridDictionary (true);
- foreach (string r in cachedArray)
- cachedRoles.Add(r, r);
+ foreach (string r in _cachedArray)
+ _cachedRoles.Add(r, r);
+
+ _listChanged = true;
}
- return cachedArray;
+ return _cachedArray;
}
public bool IsInRole (string role)
{
- if (!identity.IsAuthenticated)
+ if (!_identity.IsAuthenticated)
return false;
GetRoles ();
- return cachedRoles[role] != null;
+ return _cachedRoles [role] != null;
}
- [MonoTODO ("Not implemented")]
public string ToEncryptedTicket ()
{
- throw new NotImplementedException ();
+ string roles = string.Join (",", GetRoles ());
+ string cookiePath = RoleManagerConfig.CookiePath;
+ int approxTicketLen = roles.Length + cookiePath.Length
+ 64;
+
+ MemoryStream ticket = new MemoryStream
(approxTicketLen);
+ BinaryWriter writer = new BinaryWriter (ticket);
+
+ // version
+ writer.Write (Version);
+
+ // issue datetime
+ DateTime issueDate = DateTime.Now;
+ writer.Write (issueDate.Ticks);
+
+ // expiration datetime
+ writer.Write
(issueDate.Add(RoleManagerConfig.CookieTimeout).Ticks);
+
+ writer.Write (cookiePath);
+ writer.Write (roles);
+
+ CookieProtection cookieProtection =
RoleManagerConfig.CookieProtection;
+
+ if (cookieProtection == CookieProtection.None)
+ return GetBase64FromBytes (ticket.GetBuffer (),
0, (int) ticket.Position);
+
+ if (cookieProtection == CookieProtection.All ||
cookieProtection == CookieProtection.Validation) {
+
+ byte [] hashBytes = null;
+ byte [] validationBytes =
MachineConfig.ValidationKeyBytes;
+ writer.Write (validationBytes);
+
+ switch (MachineConfig.Validation) {
+ case MachineKeyValidation.MD5:
+ hashBytes = MD5.Create
().ComputeHash (ticket.GetBuffer (), 0, (int) ticket.Position);
+ break;
+
+ case MachineKeyValidation.TripleDES:
+ case MachineKeyValidation.SHA1:
+ hashBytes = SHA1.Create
().ComputeHash (ticket.GetBuffer (), 0, (int) ticket.Position);
+ break;
+ }
+
+ writer.Seek (-validationBytes.Length,
SeekOrigin.Current);
+ writer.Write (hashBytes);
+ }
+
+ byte [] ticketBytes = null;
+ if (cookieProtection == CookieProtection.All ||
cookieProtection == CookieProtection.Encryption) {
+ ICryptoTransform enc;
+ enc = TripleDES.Create ().CreateEncryptor
(MachineConfig.DecryptionKey192Bits, InitVector);
+ ticketBytes = enc.TransformFinalBlock
(ticket.GetBuffer (), 0, (int) ticket.Position);
+ }
+
+ if (ticketBytes == null)
+ return GetBase64FromBytes (ticket.GetBuffer (),
0, (int) ticket.Position);
+ else
+ return GetBase64FromBytes (ticketBytes, 0,
ticketBytes.Length);
}
+
+ private void DecryptTicket (string encryptedTicket)
+ {
+ if (encryptedTicket == null || encryptedTicket ==
String.Empty)
+ throw new ArgumentException ("Invalid encrypted
ticket", "encryptedTicket");
+
+ byte [] ticketBytes = GetBytesFromBase64
(encryptedTicket);
+ byte [] decryptedTicketBytes = null;
+
+ CookieProtection cookieProtection =
RoleManagerConfig.CookieProtection;
+ if (cookieProtection == CookieProtection.All ||
cookieProtection == CookieProtection.Encryption) {
+ ICryptoTransform decryptor;
+ decryptor = TripleDES.Create ().CreateDecryptor
(MachineConfig.DecryptionKey192Bits, InitVector);
+ decryptedTicketBytes =
decryptor.TransformFinalBlock (ticketBytes, 0, ticketBytes.Length);
+ }
+ else
+ decryptedTicketBytes = ticketBytes;
+
+ if (cookieProtection == CookieProtection.All ||
cookieProtection == CookieProtection.Validation) {
+ byte [] validationBytes =
MachineConfig.ValidationKeyBytes;
+ byte [] rolesWithValidationBytes = null;
+ byte [] tmpValidation = null;
+
+ int hashSize = (MachineConfig.Validation ==
MachineKeyValidation.MD5) ? 16 : 20; //md5 is 16 bytes, sha1 is 20 bytes
+
+ rolesWithValidationBytes = new byte
[decryptedTicketBytes.Length - hashSize + validationBytes.Length];
+
+ Buffer.BlockCopy (decryptedTicketBytes, 0,
rolesWithValidationBytes, 0, decryptedTicketBytes.Length - hashSize);
+ Buffer.BlockCopy (validationBytes, 0,
rolesWithValidationBytes, decryptedTicketBytes.Length - hashSize,
validationBytes.Length);
+
+ switch (MachineConfig.Validation) {
+ case MachineKeyValidation.MD5:
+ tmpValidation = MD5.Create
().ComputeHash (rolesWithValidationBytes);
+ break;
+
+ case MachineKeyValidation.TripleDES:
+ case MachineKeyValidation.SHA1:
+ tmpValidation = SHA1.Create
().ComputeHash (rolesWithValidationBytes);
+ break;
+ }
+ for (int i = 0; i < tmpValidation.Length; i++) {
+ if (i >= decryptedTicketBytes.Length ||
+ tmpValidation [i] !=
decryptedTicketBytes [i + decryptedTicketBytes.Length - hashSize])
+ throw new HttpException
("ticket validation failed");
+ }
+ }
+
+ MemoryStream ticket = new MemoryStream
(decryptedTicketBytes);
+ BinaryReader reader = new BinaryReader (ticket);
+
+ // version
+ _version = reader.ReadInt32 ();
+
+ // issued date
+ _issueDate = new DateTime (reader.ReadInt64 ());
+
+ // expire date
+ _exprireDate = new DateTime (reader.ReadInt64 ());
+
+ // roles
+ string roles = reader.ReadString ();
+ if (!Expired)
+ InitializeRoles (roles);
+
+ // cookie path
+ _cookiePath = reader.ReadString ();
+ }
+
+ private void InitializeRoles (string decryptedRoles)
+ {
+ _cachedArray = decryptedRoles.Split (',');
+ _cachedRoles = new HybridDictionary (true);
+
+ foreach (string r in _cachedArray)
+ _cachedRoles.Add (r, r);
+ }
+
+ private byte [] InitVector
+ {
+ get { return new byte [] { 1, 2, 3, 4, 5, 6, 7, 8 }; }
+ }
public bool CachedListChanged {
- get { return listChanged; }
+ get { return _listChanged; }
}
- [MonoTODO ("Not implemented")]
public string CookiePath {
- get { throw new NotImplementedException (); }
+ get { return _cookiePath; }
}
- [MonoTODO ("Not implemented")]
public bool Expired {
- get { throw new NotImplementedException (); }
+ get { return ExpireDate < DateTime.Now; }
}
- [MonoTODO ("Not implemented")]
public DateTime ExpireDate {
- get { throw new NotImplementedException (); }
+ get { return _exprireDate; }
}
public IIdentity Identity {
- get { return identity; }
+ get { return _identity; }
}
public bool IsRoleListCached {
- get { return cachedRoles != null; }
+ get { return (_cachedRoles != null) &&
RoleManagerConfig.CacheRolesInCookie; }
}
- [MonoTODO ("Not implemented")]
public DateTime IssueDate {
- get { throw new NotImplementedException (); }
+ get { return _issueDate; }
}
public string ProviderName {
- get { return String.IsNullOrEmpty(providerName) ?
Provider.Name : providerName; }
+ get { return String.IsNullOrEmpty(_providerName) ?
Provider.Name : _providerName; }
}
public int Version {
- get { return 1; }
+ get { return _version; }
}
RoleProvider Provider {
get {
- if (String.IsNullOrEmpty(providerName))
+ if (String.IsNullOrEmpty (_providerName))
return Roles.Provider;
- return Roles.Providers [providerName];
+ return Roles.Providers [_providerName];
}
}
public void SetDirty ()
{
- listChanged = true;
- cachedRoles = null;
- cachedArray = null;
+ _listChanged = true;
+ _cachedRoles = null;
+ _cachedArray = null;
}
+
+ static string GetBase64FromBytes (byte [] bytes, int offset,
int len)
+ {
+ return Convert.ToBase64String (bytes, offset, len);
+ }
+
+ static byte [] GetBytesFromBase64 (string base64String)
+ {
+ return Convert.FromBase64String (base64String);
+ }
+
+ RoleManagerSection RoleManagerConfig
+ {
+ get { return (RoleManagerSection)
WebConfigurationManager.GetSection ("system.web/roleManager"); }
+ }
+
+ MachineKeySection MachineConfig
+ {
+ get { return (MachineKeySection)
WebConfigurationManager.GetSection ("system.web/machineKey"); }
+ }
}
}
#endif
Modified: trunk/mcs/class/System.Web/System.Web.Security/Roles.cs
===================================================================
--- trunk/mcs/class/System.Web/System.Web.Security/Roles.cs 2007-02-19
12:41:26 UTC (rev 73109)
+++ trunk/mcs/class/System.Web/System.Web.Security/Roles.cs 2007-02-19
12:41:56 UTC (rev 73110)
@@ -39,27 +39,6 @@
public static class Roles {
#if TARGET_J2EE
const string Roles_cookie_protection =
"Roles.cookie_protection";
- private static CookieProtection cookie_protection {
- get {
- object o = AppDomain.CurrentDomain.GetData
(Roles_cookie_protection);
- if (o == null) {
- lock (AppDomain.CurrentDomain) {
- o =
AppDomain.CurrentDomain.GetData (Roles_cookie_protection);
- if (o == null) {
- cookie_protection =
CookieProtection.All;
- return
cookie_protection;
- }
- }
- }
-
- return (CookieProtection) o;
- }
-
- set {
- AppDomain.CurrentDomain.SetData
(Roles_cookie_protection, value);
- }
- }
-
private static RoleManagerSection config {
get {
return (RoleManagerSection)
WebConfigurationManager.GetSection ("system.web/roleManager");
@@ -83,9 +62,6 @@
static Roles ()
{
- // default values (when not supplied in web.config)
- cookie_protection = CookieProtection.All;
-
config =
(RoleManagerSection)WebConfigurationManager.GetSection
("system.web/roleManager");
}
#endif
@@ -116,11 +92,17 @@
Provider.CreateRole (rolename);
}
- [MonoTODO ("Not implemented")]
public static void DeleteCookie ()
{
- if (CacheRolesInCookie)
- throw new NotSupportedException ("Caching roles
in cookie is not supported");
+ if (CacheRolesInCookie) {
+ HttpCookie clearCookie = new HttpCookie
(CookieName, "");
+ clearCookie.Expires = DateTime.MinValue;
+ clearCookie.Path = CookiePath;
+ clearCookie.Domain = Domain;
+ clearCookie.Secure = CookieRequireSSL;
+
+ HttpContext.Current.Response.SetCookie
(clearCookie);
+ }
}
public static bool DeleteRole (string rolename)
@@ -219,9 +201,8 @@
get { return config.CookiePath; }
}
- [MonoTODO ("read infos from web.config")]
public static CookieProtection CookieProtectionValue {
- get { return cookie_protection; }
+ get { return config.CookieProtection; }
}
public static bool CookieRequireSSL {
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches