summaryrefslogtreecommitdiffstats
path: root/src/OAuth/OAuthClient/Yammer.aspx.cs
blob: 2e87a62deea305c128f62e3e90fdcfff29e9354d (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
namespace OAuthClient {
	using System;
	using System.Collections.Generic;
	using System.Configuration;
	using System.Linq;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using DotNetOpenAuth.ApplicationBlock;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth;

	public partial class Yammer : System.Web.UI.Page {
		private string RequestToken {
			get { return (string)ViewState["YammerRequestToken"]; }
			set { ViewState["YammerRequestToken"] = value; }
		}

		private string AccessToken {
			get { return (string)Session["YammerAccessToken"]; }
			set { Session["YammerAccessToken"] = value; }
		}

		private InMemoryTokenManager TokenManager {
			get {
				var tokenManager = (InMemoryTokenManager)Application["YammerTokenManager"];
				if (tokenManager == null) {
					string consumerKey = ConfigurationManager.AppSettings["YammerConsumerKey"];
					string consumerSecret = ConfigurationManager.AppSettings["YammerConsumerSecret"];
					if (!string.IsNullOrEmpty(consumerKey)) {
						tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
						Application["YammerTokenManager"] = tokenManager;
					}
				}

				return tokenManager;
			}
		}

		protected void Page_Load(object sender, EventArgs e) {
			if (this.TokenManager != null) {
				this.MultiView1.SetActiveView(this.BeginAuthorizationView);
			}
		}

		protected void getYammerMessages_Click(object sender, EventArgs e) {
			var yammer = new WebConsumer(YammerConsumer.ServiceDescription, this.TokenManager);
		}

		protected void obtainAuthorizationButton_Click(object sender, EventArgs e) {
			var yammer = YammerConsumer.CreateConsumer(this.TokenManager);
			string requestToken;
			Uri popupWindowLocation = YammerConsumer.PrepareRequestAuthorization(yammer, out requestToken);
			this.RequestToken = requestToken;
			string javascript = "window.open('" + popupWindowLocation.AbsoluteUri + "');";
			this.Page.ClientScript.RegisterStartupScript(GetType(), "YammerPopup", javascript, true);
			this.MultiView1.SetActiveView(this.CompleteAuthorizationView);
		}

		protected void finishAuthorizationButton_Click(object sender, EventArgs e) {
			if (!Page.IsValid) {
				return;
			}

			var yammer = YammerConsumer.CreateConsumer(this.TokenManager);
			var authorizationResponse = YammerConsumer.CompleteAuthorization(yammer, this.RequestToken, this.yammerUserCode.Text);
			if (authorizationResponse != null) {
				this.accessTokenLabel.Text = HttpUtility.HtmlEncode(authorizationResponse.AccessToken);
				this.MultiView1.SetActiveView(this.AuthorizationCompleteView);
			} else {
				this.MultiView1.SetActiveView(this.BeginAuthorizationView);
				this.authorizationErrorLabel.Visible = true;
			}
		}
	}
}