summaryrefslogtreecommitdiffstats
path: root/samples/ProviderPortal/Code/URLRewriter.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2009-01-31 21:32:01 -0800
committerAndrew <andrewarnott@gmail.com>2009-01-31 21:32:01 -0800
commite865ec7da293496d6c2e67039b86c4ef9ded5a6c (patch)
tree116d6a4e8c0892d2be13acd31bacae004de4bb8b /samples/ProviderPortal/Code/URLRewriter.cs
parentf6d5493a42d07389421763c2f1da35b4ca7d9f45 (diff)
downloadDotNetOpenAuth-e865ec7da293496d6c2e67039b86c4ef9ded5a6c.zip
DotNetOpenAuth-e865ec7da293496d6c2e67039b86c4ef9ded5a6c.tar.gz
DotNetOpenAuth-e865ec7da293496d6c2e67039b86c4ef9ded5a6c.tar.bz2
Added OpenID sample sites: RP MVC, RP WebForms, RP Classic ASP, OP WebForms.
Diffstat (limited to 'samples/ProviderPortal/Code/URLRewriter.cs')
-rw-r--r--samples/ProviderPortal/Code/URLRewriter.cs61
1 files changed, 61 insertions, 0 deletions
diff --git a/samples/ProviderPortal/Code/URLRewriter.cs b/samples/ProviderPortal/Code/URLRewriter.cs
new file mode 100644
index 0000000..daa4dea
--- /dev/null
+++ b/samples/ProviderPortal/Code/URLRewriter.cs
@@ -0,0 +1,61 @@
+namespace OpenIdProviderWebForms.Code {
+ using System.Configuration;
+ using System.Diagnostics;
+ using System.Text.RegularExpressions;
+ using System.Web;
+ using System.Xml;
+
+ // nicked from http://www.codeproject.com/aspnet/URLRewriter.asp
+ public class URLRewriter : IConfigurationSectionHandler {
+ public static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
+
+ protected XmlNode rules = null;
+
+ protected URLRewriter() {
+ }
+
+ public static void Process() {
+ URLRewriter rewriter = (URLRewriter)ConfigurationManager.GetSection("urlrewrites");
+
+ string subst = rewriter.GetSubstitution(HttpContext.Current.Request.Path);
+
+ if (!string.IsNullOrEmpty(subst)) {
+ Logger.InfoFormat("Rewriting url '{0}' to '{1}' ", HttpContext.Current.Request.Path, subst);
+ HttpContext.Current.RewritePath(subst);
+ }
+ }
+
+ public string GetSubstitution(string path) {
+ foreach (XmlNode node in this.rules.SelectNodes("rule")) {
+ // get the url and rewrite nodes
+ XmlNode urlNode = node.SelectSingleNode("url");
+ XmlNode rewriteNode = node.SelectSingleNode("rewrite");
+
+ // check validity of the values
+ if (urlNode == null || string.IsNullOrEmpty(urlNode.InnerText)
+ || rewriteNode == null || string.IsNullOrEmpty(rewriteNode.InnerText)) {
+ Logger.Warn("Invalid urlrewrites rule discovered in web.config file.");
+ continue;
+ }
+
+ Regex reg = new Regex(urlNode.InnerText, RegexOptions.IgnoreCase);
+
+ // if match, return the substitution
+ Match match = reg.Match(path);
+ if (match.Success) {
+ return reg.Replace(path, rewriteNode.InnerText);
+ }
+ }
+
+ return null; // no rewrite
+ }
+
+ #region Implementation of IConfigurationSectionHandler
+ public object Create(object parent, object configContext, XmlNode section) {
+ this.rules = section;
+
+ return this;
+ }
+ #endregion
+ }
+} \ No newline at end of file