public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { Debug.Assert(context != null); SecUtility.CheckParameter(ref id, true, true, false, Configuration.MaxStringPropertySizeInChars, "id"); _providerRetry(() => { TableServiceContext svc = CreateDataServiceContext(); SessionRow session; if (!newItem) { session = GetSession(id, svc); if (session == null || session.Lock != (int)lockId) { Debug.Assert(false); return; } } else { session = new SessionRow(id, _applicationName); session.Lock = 1; session.LockDateUtc = DateTime.UtcNow; } session.Initialized = true; Debug.Assert(session.Timeout >= 0); session.Timeout = item.Timeout; session.ExpiresUtc = DateTime.UtcNow.AddMinutes(session.Timeout); session.Locked = false; // yes, we always create a new blob here session.BlobName = GetBlobNamePrefix(id) + Guid.NewGuid().ToString("N"); // Serialize the session and write the blob byte[] items, statics; SerializeSession(item, out items, out statics); string serializedItems = Convert.ToBase64String(items); string serializedStatics = Convert.ToBase64String(statics); MemoryStream output = new MemoryStream(); StreamWriter writer = new StreamWriter(output); try { writer.WriteLine(serializedItems); writer.WriteLine(serializedStatics); writer.Flush(); // for us, it shouldn't matter whether newItem is set to true or false // because we always create the entire blob and cannot append to an // existing one _blobProvider.UploadStream(session.BlobName, output); writer.Close(); output.Close(); } catch (Exception e) { if (!newItem) { ReleaseItemExclusive(svc, session, lockId); } throw new ProviderException("Error accessing the data store.", e); } finally { if (writer != null) { writer.Close(); } if (output != null) { output.Close(); } } if (newItem) { svc.AddObject(_tableName, session); svc.SaveChangesWithRetries(); } else { // Unlock the session and save changes ReleaseItemExclusive(svc, session, lockId); } }); }
public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection) { if (collection == null) { throw new ArgumentNullException("collection"); } if (context == null) { throw new ArgumentNullException("context"); } // Get information about the user who owns the profile string username = (string)context["UserName"]; bool authenticated = (bool)context["IsAuthenticated"]; if (String.IsNullOrEmpty(username) || collection.Count == 0) { return; } if (!VerifyUsername(ref username)) { return; } // lets create the blob containing the profile without checking whether the user exists // Format the profile data for saving string names = String.Empty; string values = String.Empty; byte[] buf = null; EncodeProfileData(ref names, ref values, ref buf, collection, authenticated); // Do nothing if no properties need saving if (string.IsNullOrEmpty(names)) { return; } // Save the profile data MemoryStream stream = null; StreamWriter writer = null; string blobName = null; try { stream = new MemoryStream(); writer = new StreamWriter(stream); writer.WriteLine(names); if (!String.IsNullOrEmpty(values)) { UnicodeEncoding encoding = new UnicodeEncoding(); writer.WriteLine(Convert.ToBase64String(encoding.GetBytes(values))); } else { writer.WriteLine(); } if (buf != null && buf.Length > 0) { writer.WriteLine(Convert.ToBase64String(buf)); } else { writer.WriteLine(); } writer.Flush(); blobName = GetProfileBlobPrefix(username) + Guid.NewGuid().ToString("N"); Debug.Assert(blobName.Length <= Configuration.MaxStringPropertySizeInChars); stream.Seek(0, SeekOrigin.Begin); _blobProvider.UploadStream(blobName, stream); CreateOrUpdateUserAndProfile(username, authenticated, DateTime.UtcNow, blobName, (int)stream.Length); } catch (Exception e) { throw new ProviderException("Error writing property values!", e); } finally { if (writer != null) { writer.Close(); } if (stream != null) { stream.Close(); } } }