|
| 1 | +//----------------------------------------------------------------------- |
| 2 | +// <copyright file="CustomStore.cs" company="Andrew Arnott"> |
| 3 | +// Copyright (c) Andrew Arnott. All rights reserved. |
| 4 | +// </copyright> |
| 5 | +//----------------------------------------------------------------------- |
| 6 | + |
| 7 | +namespace OpenIdProviderWebForms.Code { |
| 8 | + using System; |
| 9 | + using System.Data; |
| 10 | + using System.Globalization; |
| 11 | + using System.Security.Cryptography; |
| 12 | + using DotNetOpenAuth.OpenId; |
| 13 | + using IProviderAssociationStore = DotNetOpenAuth.OpenId.IAssociationStore<DotNetOpenAuth.OpenId.AssociationRelyingPartyType>; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// This custom store serializes all elements to demonstrate peristent and/or shared storage. |
| 17 | + /// This is common in a web farm, for example. |
| 18 | + /// </summary> |
| 19 | + /// <remarks> |
| 20 | + /// This doesn't actually serialize anything to a persistent store, so restarting the web server |
| 21 | + /// will still clear everything this store is supposed to remember. |
| 22 | + /// But we "persist" all associations and nonces into a DataTable to demonstrate |
| 23 | + /// that using a database is possible. |
| 24 | + /// </remarks> |
| 25 | + public class CustomStore : IProviderAssociationStore { |
| 26 | + private static CustomStoreDataSet dataSet = new CustomStoreDataSet(); |
| 27 | + |
| 28 | + #region IAssociationStore<AssociationRelyingPartyType> Members |
| 29 | + |
| 30 | + public void StoreAssociation(AssociationRelyingPartyType distinguishingFactor, Association assoc) { |
| 31 | + var assocRow = dataSet.Association.NewAssociationRow(); |
| 32 | + assocRow.DistinguishingFactor = distinguishingFactor.ToString(); |
| 33 | + assocRow.Handle = assoc.Handle; |
| 34 | + assocRow.Expires = assoc.Expires.ToLocalTime(); |
| 35 | + assocRow.PrivateData = assoc.SerializePrivateData(); |
| 36 | + dataSet.Association.AddAssociationRow(assocRow); |
| 37 | + } |
| 38 | + |
| 39 | + public Association GetAssociation(AssociationRelyingPartyType distinguishingFactor) { |
| 40 | + // properly escape the URL to prevent injection attacks. |
| 41 | + string value = distinguishingFactor.ToString(); |
| 42 | + string filter = string.Format( |
| 43 | + CultureInfo.InvariantCulture, |
| 44 | + "{0} = '{1}'", |
| 45 | + dataSet.Association.DistinguishingFactorColumn.ColumnName, |
| 46 | + value); |
| 47 | + string sort = dataSet.Association.ExpiresColumn.ColumnName + " DESC"; |
| 48 | + DataView view = new DataView(dataSet.Association, filter, sort, DataViewRowState.CurrentRows); |
| 49 | + if (view.Count == 0) { |
| 50 | + return null; |
| 51 | + } |
| 52 | + var row = (CustomStoreDataSet.AssociationRow)view[0].Row; |
| 53 | + return Association.Deserialize(row.Handle, row.Expires.ToUniversalTime(), row.PrivateData); |
| 54 | + } |
| 55 | + |
| 56 | + public Association GetAssociation(AssociationRelyingPartyType distinguishingFactor, string handle) { |
| 57 | + var assocRow = dataSet.Association.FindByDistinguishingFactorHandle(distinguishingFactor.ToString(), handle); |
| 58 | + return Association.Deserialize(assocRow.Handle, assocRow.Expires, assocRow.PrivateData); |
| 59 | + } |
| 60 | + |
| 61 | + public bool RemoveAssociation(AssociationRelyingPartyType distinguishingFactor, string handle) { |
| 62 | + var row = dataSet.Association.FindByDistinguishingFactorHandle(distinguishingFactor.ToString(), handle); |
| 63 | + if (row != null) { |
| 64 | + dataSet.Association.RemoveAssociationRow(row); |
| 65 | + return true; |
| 66 | + } else { |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void ClearExpiredAssociations() { |
| 72 | + this.removeExpiredRows(dataSet.Association, dataSet.Association.ExpiresColumn.ColumnName); |
| 73 | + } |
| 74 | + |
| 75 | + #endregion |
| 76 | + |
| 77 | + private void removeExpiredRows(DataTable table, string expiredColumnName) { |
| 78 | + string filter = string.Format( |
| 79 | + CultureInfo.InvariantCulture, |
| 80 | + "{0} < #{1}#", |
| 81 | + expiredColumnName, |
| 82 | + DateTime.Now); |
| 83 | + DataView view = new DataView(table, filter, null, DataViewRowState.CurrentRows); |
| 84 | + for (int i = view.Count - 1; i >= 0; i--) { |
| 85 | + view.Delete(i); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments