Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Extensions / ClientServices / Providers / ClientData.cs / 1305376 / ClientData.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.ClientServices.Providers { using System; using System.Security; using System.Security.Permissions; using System.Security.AccessControl; using System.Globalization; using System.Data; using System.Data.Common; using System.Data.OleDb; using System.IO; using System.Windows.Forms; using System.Data.SqlClient; using System.Xml.Serialization; using System.Diagnostics.CodeAnalysis; using System.Xml.Schema; using System.Xml; using System.Collections.Specialized; using System.IO.IsolatedStorage; internal class ClientData { internal enum ClientDateStoreOrderEnum { LastLoggedInUserName = 0, LastLoggedInDateUtc = 1, PasswordHash = 2, PasswordSalt = 3, Roles = 4, RolesCachedDateUtc = 5, SettingsNames = 6, SettingsStoredAs = 7, SettingsValues = 8, SettingsNeedReset = 9, SettingsCacheIsMoreFresh = 10, CookieNames = 11, CookieValues = 12 } private const int _NumStoredValues = 13; private static string[] _StoredValueNames = new string[_NumStoredValues] { "LastLoggedInUserName", "LastLoggedInDateUtc", "PasswordHash", "PasswordSalt", "Roles", "RolesCachedDateUtc", "SettingsNames", "SettingsStoredAs", "SettingsValues", "SettingsNeedReset", "SettingsCacheIsMoreFresh", "CookieNames", "CookieValues"}; private object[] _StoredValues = new object[_NumStoredValues] { "", DateTime.UtcNow.AddYears(-1), string.Empty, string.Empty, new string[0], DateTime.UtcNow.AddYears(-1), new string[0], new string[0], new string[0], false, false, new string[0], new string[0]}; private ClientData() { } private ClientData(XmlReader reader) { reader.ReadStartElement("ClientData"); for (int iter = 0; iter < _NumStoredValues; iter++) { reader.ReadStartElement(_StoredValueNames[iter]); if (_StoredValues[iter] is string) _StoredValues[iter] = reader.ReadContentAsString(); else if (_StoredValues[iter] is DateTime) { string s = reader.ReadContentAsString(); long l = long.Parse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture); _StoredValues[iter] = DateTime.FromFileTimeUtc(l); } else if (_StoredValues[iter] is bool) { string s = reader.ReadContentAsString(); _StoredValues[iter] = !(string.IsNullOrEmpty(s) || s != "1"); } else { _StoredValues[iter] = ReadStringArray(reader); } reader.ReadEndElement(); } reader.ReadEndElement(); } private static string[] ReadStringArray(XmlReader reader) { //string count = reader.GetAttribute("count"); //if (string.IsNullOrEmpty(count)) // return new string[0]; StringCollection sc = new StringCollection(); while (reader.IsStartElement()) { reader.ReadStartElement("item"); sc.Add(reader.ReadContentAsString()); reader.ReadEndElement(); } string[] retValue = new string[sc.Count]; sc.CopyTo(retValue, 0); return retValue; } private static void WriteStringArray(XmlWriter writer, string [] arrToWrite) { //writer.WriteAttributeString("count", arrToWrite.Length.ToString()); if (arrToWrite.Length == 0) writer.WriteValue(string.Empty); for (int iter = 0; iter < arrToWrite.Length; iter++) { writer.WriteStartElement("item"); writer.WriteValue((arrToWrite[iter]==null) ? string.Empty : arrToWrite[iter]); writer.WriteEndElement(); } } // App data internal string LastLoggedInUserName { get { return (string)_StoredValues[(int)ClientDateStoreOrderEnum.LastLoggedInUserName]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.LastLoggedInUserName] = value; } } internal DateTime LastLoggedInDateUtc { get { return (DateTime)_StoredValues[(int)ClientDateStoreOrderEnum.LastLoggedInDateUtc]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.LastLoggedInDateUtc] = value; } } // Membership data internal string PasswordHash { get { return (string)_StoredValues[(int)ClientDateStoreOrderEnum.PasswordHash]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.PasswordHash] = value; } } internal string PasswordSalt { get { return (string)_StoredValues[(int)ClientDateStoreOrderEnum.PasswordSalt]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.PasswordSalt] = value; } } // Roles data internal string[] Roles { get { return (string[])_StoredValues[(int)ClientDateStoreOrderEnum.Roles]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.Roles] = value; } } internal DateTime RolesCachedDateUtc { get { return (DateTime)_StoredValues[(int)ClientDateStoreOrderEnum.RolesCachedDateUtc]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.RolesCachedDateUtc] = value; } } // Settings data internal string[] SettingsNames { get { return (string[])_StoredValues[(int)ClientDateStoreOrderEnum.SettingsNames]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.SettingsNames] = value; } } internal string[] SettingsStoredAs{ get { return (string[])_StoredValues[(int)ClientDateStoreOrderEnum.SettingsStoredAs]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.SettingsStoredAs] = value; } } internal string[] SettingsValues { get { return (string[])_StoredValues[(int)ClientDateStoreOrderEnum.SettingsValues]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.SettingsValues] = value; } } internal bool SettingsNeedReset { get { return (bool)_StoredValues[(int)ClientDateStoreOrderEnum.SettingsNeedReset]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.SettingsNeedReset] = value; } } internal bool SettingsCacheIsMoreFresh { get { return (bool)_StoredValues[(int)ClientDateStoreOrderEnum.SettingsCacheIsMoreFresh]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.SettingsCacheIsMoreFresh] = value; } } // Cookie data internal string[] CookieNames { get { return (string[])_StoredValues[(int)ClientDateStoreOrderEnum.CookieNames]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.CookieNames] = value; } } internal string[] CookieValues { get { return (string[])_StoredValues[(int)ClientDateStoreOrderEnum.CookieValues]; } set { _StoredValues[(int)ClientDateStoreOrderEnum.CookieValues] = value; } } // Filename private string FileName = string.Empty; private bool UsingIsolatedStorage = false; private const string _IsolatedDir = "System.Web.Extensions.ClientServices.ClientData"; internal void Save() { if (!UsingIsolatedStorage) { using (XmlWriter writer = XmlWriter.Create(FileName)) { Save(writer); } } else { using(IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly()) { if (f.GetDirectoryNames(_IsolatedDir).Length == 0) f.CreateDirectory(_IsolatedDir); using(IsolatedStorageFileStream fs = new IsolatedStorageFileStream(FileName, FileMode.Create, f)) { using (XmlWriter writer = XmlWriter.Create(fs)) { Save(writer); } } } } } private void Save(XmlWriter writer) { writer.WriteStartElement("ClientData"); for (int iter = 0; iter < _NumStoredValues; iter++) { writer.WriteStartElement(_StoredValueNames[iter]); if (_StoredValues[iter] == null) { writer.WriteValue(string.Empty); } else if (_StoredValues[iter] is string) { writer.WriteValue(_StoredValues[iter]); } else if (_StoredValues[iter] is bool) { writer.WriteValue(((bool)_StoredValues[iter]) ? "1" : "0"); } else if (_StoredValues[iter] is DateTime) { writer.WriteValue(((DateTime)_StoredValues[iter]).ToFileTimeUtc().ToString("X", CultureInfo.InvariantCulture)); } else { WriteStringArray(writer, (string[])_StoredValues[iter]); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.Flush(); } internal static ClientData Load(string username, bool useIsolatedStorage) { ClientData cd = null; string fileName = null; if (useIsolatedStorage) { fileName = _IsolatedDir + "\\" + SqlHelper.GetPartialDBFileName(username, ".clientdata"); try { using(IsolatedStorageFile f = IsolatedStorageFile.GetUserStoreForAssembly()) { using(IsolatedStorageFileStream fs = new IsolatedStorageFileStream(fileName, FileMode.Open, f)) { using (XmlReader xr = XmlReader.Create(fs)) { cd = new ClientData(xr); } } } } catch {} // ignore exceptions } else { fileName = SqlHelper.GetFullDBFileName(username, ".clientdata"); try { if (File.Exists(fileName)) { using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { using (XmlReader xr = XmlReader.Create(fs)) { cd = new ClientData(xr); } } } } catch {} // ignore exceptions } if (cd == null) cd = new ClientData(); cd.UsingIsolatedStorage = useIsolatedStorage; cd.FileName = fileName; return cd; } } internal static class ClientDataManager { static private ClientData _applicationClientData; static private ClientData _userClientData; static private string _curUserName; internal static ClientData GetAppClientData(bool useIsolatedStore) { if (_applicationClientData == null) _applicationClientData = ClientData.Load(null, useIsolatedStore); return _applicationClientData; } internal static ClientData GetUserClientData(string username, bool useIsolatedStore) { if (username != _curUserName) { _curUserName = username; _userClientData = ClientData.Load(username, useIsolatedStore); } return _userClientData; } internal static string GetCookie(string username, string cookieName, bool useIsolatedStore) { ClientData cd = GetUserClientData(username, useIsolatedStore); if (cd.CookieNames == null) { cd.CookieNames = new string[0]; cd.CookieValues = new string[0]; return null; } for(int iter=0; iter
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DataGridViewCellCollection.cs
- SafeFileHandle.cs
- ConfigurationLockCollection.cs
- KeysConverter.cs
- ActivityStateRecord.cs
- NamespaceTable.cs
- DocumentOutline.cs
- TextLineBreak.cs
- PersianCalendar.cs
- NativeCppClassAttribute.cs
- CheckBoxStandardAdapter.cs
- BuildProviderCollection.cs
- UserMapPath.cs
- SchemaConstraints.cs
- SelectionUIService.cs
- ToolboxItem.cs
- TextRange.cs
- SystemIPInterfaceProperties.cs
- TextContainerChangeEventArgs.cs
- RunInstallerAttribute.cs
- RegistryConfigurationProvider.cs
- FileAuthorizationModule.cs
- AxHost.cs
- PageAdapter.cs
- InstancePersistenceException.cs
- CellNormalizer.cs
- XPathNodeHelper.cs
- CallInfo.cs
- DecoderNLS.cs
- AtomMaterializerLog.cs
- QuaternionKeyFrameCollection.cs
- DBCommandBuilder.cs
- CryptoStream.cs
- StructuralComparisons.cs
- BuildManager.cs
- Encoder.cs
- TypeDescriptionProvider.cs
- DisplayNameAttribute.cs
- ZoomPercentageConverter.cs
- LoginNameDesigner.cs
- XmlSchemaProviderAttribute.cs
- AnimatedTypeHelpers.cs
- TemplateApplicationHelper.cs
- XmlEnumAttribute.cs
- ObjectTag.cs
- FontFaceLayoutInfo.cs
- ToolStripOverflow.cs
- ProtocolElementCollection.cs
- RegexWorker.cs
- RouteValueExpressionBuilder.cs
- OdbcInfoMessageEvent.cs
- TreeViewHitTestInfo.cs
- PinnedBufferMemoryStream.cs
- SemaphoreSlim.cs
- RoleServiceManager.cs
- IsolatedStorageFile.cs
- TextViewSelectionProcessor.cs
- SafeBitVector32.cs
- BitmapEffectInput.cs
- SyntaxCheck.cs
- ToolStripItem.cs
- cookiecontainer.cs
- BindingCompleteEventArgs.cs
- Nullable.cs
- DefinitionUpdate.cs
- HttpCacheVaryByContentEncodings.cs
- TextContainerChangedEventArgs.cs
- DisposableCollectionWrapper.cs
- XmlWrappingReader.cs
- EntityContainerEntitySetDefiningQuery.cs
- UnmanagedMarshal.cs
- ComplexTypeEmitter.cs
- MruCache.cs
- HexParser.cs
- EnumMember.cs
- IRCollection.cs
- SiteMapNode.cs
- Point3D.cs
- EntityDataSourceReferenceGroup.cs
- XMLUtil.cs
- Vector3DKeyFrameCollection.cs
- DataGridColumnCollection.cs
- DependencyPropertyKind.cs
- _OverlappedAsyncResult.cs
- SchemaElement.cs
- Application.cs
- MessagePartProtectionMode.cs
- IisTraceListener.cs
- VectorConverter.cs
- SystemResourceKey.cs
- SafeNativeMethods.cs
- SessionSwitchEventArgs.cs
- ColorTypeConverter.cs
- IListConverters.cs
- EventHandlersStore.cs
- MergeLocalizationDirectives.cs
- PrintSystemException.cs
- ObservableDictionary.cs
- WebControlParameterProxy.cs
- SecurityKeyIdentifier.cs