summaryrefslogtreecommitdiffstats
path: root/samples/OpenIdRelyingPartyWebForms/Global.asax.cs
blob: 6583289d5d0aa28ee04fc68ee72d4b61d1eaf64f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
namespace OpenIdRelyingPartyWebForms {
	using System;
	using System.Collections.Specialized;
	using System.Configuration;
	using System.IO;
	using System.Text;
	using System.Web;
	using DotNetOpenAuth.ApplicationBlock;
	using DotNetOpenAuth.OAuth;
	using OpenIdRelyingPartyWebForms.Code;

	public class Global : HttpApplication {
		public static log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(Global));

		internal static StringBuilder LogMessages = new StringBuilder();

		internal static WebConsumer GoogleWebConsumer {
			get {
				var googleWebConsumer = (WebConsumer)HttpContext.Current.Application["GoogleWebConsumer"];
				if (googleWebConsumer == null) {
					googleWebConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, GoogleTokenManager);
					HttpContext.Current.Application["GoogleWebConsumer"] = googleWebConsumer;
				}

				return googleWebConsumer;
			}
		}

		internal static InMemoryTokenManager GoogleTokenManager {
			get {
				var tokenManager = (InMemoryTokenManager)HttpContext.Current.Application["GoogleTokenManager"];
				if (tokenManager == null) {
					string consumerKey = ConfigurationManager.AppSettings["googleConsumerKey"];
					string consumerSecret = ConfigurationManager.AppSettings["googleConsumerSecret"];
					if (!string.IsNullOrEmpty(consumerKey)) {
						tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
						HttpContext.Current.Application["GoogleTokenManager"] = tokenManager;
					}
				}

				return tokenManager;
			}
		}

		internal static InMemoryTokenManager OwnSampleOPHybridTokenManager {
			get {
				var tokenManager = (InMemoryTokenManager)HttpContext.Current.Application["OwnSampleOPHybridTokenManager"];
				if (tokenManager == null) {
					string consumerKey = new Uri(HttpContext.Current.Request.Url, HttpContext.Current.Request.ApplicationPath).AbsoluteUri;
					string consumerSecret = "some crazy secret";
					tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
					HttpContext.Current.Application["OwnSampleOPHybridTokenManager"] = tokenManager;
				}

				return tokenManager;
			}
		}

		public static string ToString(NameValueCollection collection) {
			using (StringWriter sw = new StringWriter()) {
				foreach (string key in collection.Keys) {
					sw.WriteLine("{0} = '{1}'", key, collection[key]);
				}
				return sw.ToString();
			}
		}

		protected void Application_Start(object sender, EventArgs e) {
			log4net.Config.XmlConfigurator.Configure();
			Logger.Info("Sample starting...");
		}

		protected void Application_End(object sender, EventArgs e) {
			Logger.Info("Sample shutting down...");

			// this would be automatic, but in partial trust scenarios it is not.
			log4net.LogManager.Shutdown();
		}

		protected void Application_BeginRequest(object sender, EventArgs e) {
			// System.Diagnostics.Debugger.Launch();
			Logger.DebugFormat("Processing {0} on {1} ", Request.HttpMethod, stripQueryString(Request.Url));
			if (Request.QueryString.Count > 0) {
				Logger.DebugFormat("Querystring follows: \n{0}", ToString(Request.QueryString));
			}
			if (Request.Form.Count > 0) {
				Logger.DebugFormat("Posted form follows: \n{0}", ToString(Request.Form));
			}
		}

		protected void Application_AuthenticateRequest(object sender, EventArgs e) {
			Logger.DebugFormat("User {0} authenticated.", HttpContext.Current.User != null ? "IS" : "is NOT");
		}

		protected void Application_EndRequest(object sender, EventArgs e) {
		}

		protected void Application_Error(object sender, EventArgs e) {
			Logger.ErrorFormat("An unhandled exception was raised. Details follow: {0}", HttpContext.Current.Server.GetLastError());
		}

		private static string stripQueryString(Uri uri) {
			UriBuilder builder = new UriBuilder(uri);
			builder.Query = null;
			return builder.ToString();
		}
	}
}