summaryrefslogtreecommitdiffstats
path: root/samples/OAuthConsumer
diff options
context:
space:
mode:
Diffstat (limited to 'samples/OAuthConsumer')
-rw-r--r--samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs4
-rw-r--r--samples/OAuthConsumer/GoogleAddressBook.aspx57
-rw-r--r--samples/OAuthConsumer/GoogleAddressBook.aspx.cs82
-rw-r--r--samples/OAuthConsumer/Twitter.aspx19
-rw-r--r--samples/OAuthConsumer/Twitter.aspx.cs35
-rw-r--r--samples/OAuthConsumer/Web.config4
6 files changed, 98 insertions, 103 deletions
diff --git a/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs b/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs
index e2b2876..fede300 100644
--- a/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs
+++ b/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs
@@ -14,6 +14,10 @@ public class InMemoryTokenManager : IConsumerTokenManager {
private Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();
public InMemoryTokenManager(string consumerKey, string consumerSecret) {
+ if (String.IsNullOrEmpty(consumerKey)) {
+ throw new ArgumentNullException("consumerKey");
+ }
+
this.ConsumerKey = consumerKey;
this.ConsumerSecret = consumerSecret;
}
diff --git a/samples/OAuthConsumer/GoogleAddressBook.aspx b/samples/OAuthConsumer/GoogleAddressBook.aspx
index 1c20954..56179b7 100644
--- a/samples/OAuthConsumer/GoogleAddressBook.aspx
+++ b/samples/OAuthConsumer/GoogleAddressBook.aspx
@@ -1,45 +1,26 @@
-<%@ Page Title="Gmail address book demo" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
- CodeFile="GoogleAddressBook.aspx.cs" Inherits="GoogleAddressBook" %>
+<%@ Page Title="Gmail address book demo" Language="C#" MasterPageFile="~/MasterPage.master"
+ AutoEventWireup="true" CodeFile="GoogleAddressBook.aspx.cs" Inherits="GoogleAddressBook" %>
<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server">
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
- <asp:View runat="server" ID="Authorize">
- <table>
- <tr>
- <td>
- Google Consumer Key
- </td>
- <td>
- <asp:TextBox ID="consumerKeyBox" runat="server" Columns="35"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
- ControlToValidate="consumerKeyBox" Display="Dynamic"
- ErrorMessage="RequiredFieldValidator">*</asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- Google Consumer Secret
- </td>
- <td>
- <asp:TextBox ID="consumerSecretBox" runat="server" Columns="35"></asp:TextBox>
- <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
- ControlToValidate="consumerSecretBox" Display="Dynamic">*</asp:RequiredFieldValidator>
- </td>
- </tr>
- <tr>
- <td>
- &nbsp;</td>
- <td>
- Don&#39;t have a Google Consumer Key?&nbsp;
- <a href="https://www.google.com/accounts/ManageDomains">Get one</a>.</td>
- </tr>
- </table>
- <asp:Button ID="authorizeButton" runat="server" Text="Download your Gmail Address Book"
- OnClick="authorizeButton_Click" />
+ <asp:View runat="server">
+ <h2>Google setup</h2>
+ <p>A Google client app must be endorsed by a Google user. </p>
+ <ol>
+ <li><a target="_blank" href="https://www.google.com/accounts/ManageDomains">Visit Google
+ and create a client app</a>. </li>
+ <li>Modify your web.config file to include your consumer key and consumer secret.
+ </li>
+ </ol>
</asp:View>
- <asp:View runat="server" ID="Results">
- <p>Now displaying the first 25 records from your address book:</p>
- <asp:PlaceHolder runat="server" ID="resultsPlaceholder" />
+ <asp:View runat="server">
+ <h2>Updates</h2>
+ <p>Ok, Google has authorized us to download your contacts. Click &#39;Get address book&#39;
+ to download the first 25 contacts to this sample. Notice how we never asked you
+ for your Google username or password. </p>
+ <asp:Button ID="getAddressBookButton" runat="server" OnClick="getAddressBookButton_Click"
+ Text="Get address book" />
+ <asp:PlaceHolder ID="resultsPlaceholder" runat="server" />
</asp:View>
</asp:MultiView>
</asp:Content>
diff --git a/samples/OAuthConsumer/GoogleAddressBook.aspx.cs b/samples/OAuthConsumer/GoogleAddressBook.aspx.cs
index f4aa090..2613b85 100644
--- a/samples/OAuthConsumer/GoogleAddressBook.aspx.cs
+++ b/samples/OAuthConsumer/GoogleAddressBook.aspx.cs
@@ -1,4 +1,5 @@
using System;
+using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
@@ -12,47 +13,64 @@ using DotNetOpenAuth.OAuth;
/// A page to demonstrate downloading a Gmail address book using OAuth.
/// </summary>
public partial class GoogleAddressBook : System.Web.UI.Page {
+ private string AccessToken {
+ get { return (string)Session["GoogleAccessToken"]; }
+ set { Session["GoogleAccessToken"] = value; }
+ }
+
+ private InMemoryTokenManager TokenManager {
+ get {
+ var tokenManager = (InMemoryTokenManager)Application["GoogleTokenManager"];
+ if (tokenManager == null) {
+ string consumerKey = ConfigurationManager.AppSettings["googleConsumerKey"];
+ string consumerSecret = ConfigurationManager.AppSettings["googleConsumerSecret"];
+ if (!string.IsNullOrEmpty(consumerKey)) {
+ tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
+ Application["GoogleTokenManager"] = tokenManager;
+ }
+ }
+
+ return tokenManager;
+ }
+ }
+
protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- if (Session["TokenManager"] != null) {
- InMemoryTokenManager tokenManager = (InMemoryTokenManager)Session["TokenManager"];
- var google = new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
+ if (this.TokenManager != null) {
+ MultiView1.ActiveViewIndex = 1;
+
+ if (!IsPostBack) {
+ var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager);
+ // Is Google calling back with authorization?
var accessTokenResponse = google.ProcessUserAuthorization();
if (accessTokenResponse != null) {
- // User has approved access
- MultiView1.ActiveViewIndex = 1;
- resultsPlaceholder.Controls.Add(new Label { Text = accessTokenResponse.AccessToken });
-
- XDocument contactsDocument = GoogleConsumer.GetContacts(google, accessTokenResponse.AccessToken);
- var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom"))
- select new {
- Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value,
- Email = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value,
- };
- StringBuilder tableBuilder = new StringBuilder();
- tableBuilder.Append("<table><tr><td>Name</td><td>Email</td></tr>");
- foreach (var contact in contacts) {
- tableBuilder.AppendFormat(
- "<tr><td>{0}</td><td>{1}</td></tr>",
- HttpUtility.HtmlEncode(contact.Name),
- HttpUtility.HtmlEncode(contact.Email));
- }
- tableBuilder.Append("</table>");
- resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() });
+ AccessToken = accessTokenResponse.AccessToken;
+ } else if (this.AccessToken == null) {
+ // If we don't yet have access, immediately request it.
+ GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts);
}
}
}
}
- protected void authorizeButton_Click(object sender, EventArgs e) {
- if (!Page.IsValid) {
- return;
- }
+ protected void getAddressBookButton_Click(object sender, EventArgs e) {
+ var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager);
- InMemoryTokenManager tokenManager = new InMemoryTokenManager(consumerKeyBox.Text, consumerSecretBox.Text);
- Session["TokenManager"] = tokenManager;
- var google = new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
- GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts);
+ XDocument contactsDocument = GoogleConsumer.GetContacts(google, AccessToken);
+ var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom"))
+ select new {
+ Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value,
+ Email = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value,
+ };
+ StringBuilder tableBuilder = new StringBuilder();
+ tableBuilder.Append("<table><tr><td>Name</td><td>Email</td></tr>");
+ foreach (var contact in contacts) {
+ tableBuilder.AppendFormat(
+ "<tr><td>{0}</td><td>{1}</td></tr>",
+ HttpUtility.HtmlEncode(contact.Name),
+ HttpUtility.HtmlEncode(contact.Email));
+ }
+ tableBuilder.Append("</table>");
+ resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() });
}
}
diff --git a/samples/OAuthConsumer/Twitter.aspx b/samples/OAuthConsumer/Twitter.aspx
index 2a36b84..a659533 100644
--- a/samples/OAuthConsumer/Twitter.aspx
+++ b/samples/OAuthConsumer/Twitter.aspx
@@ -5,10 +5,14 @@
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server">
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
- <asp:View runat="server">
- <p>May we download your Twitter feeds? Click Authorize to let Twitter know you're ok
- with this. </p>
- <asp:Button ID="authorizeButton" runat="server" Text="Authorize" OnClick="authorizeButton_Click" />
+ <asp:View ID="View1" runat="server">
+ <h2>Twitter setup</h2>
+ <p>A Twitter client app must be endorsed by a Twitter user. </p>
+ <ol>
+ <li><a target="_blank" href="https://twitter.com/oauth_clients">Visit Twitter and create
+ a client app</a>. </li>
+ <li>Modify your web.config file to include your consumer key and consumer secret.</li>
+ </ol>
</asp:View>
<asp:View runat="server">
<h2>Updates</h2>
@@ -18,12 +22,5 @@
<asp:Button ID="downloadUpdates" runat="server" Text="Get updates" OnClick="downloadUpdates_Click" />
<asp:PlaceHolder runat="server" ID="resultsPlaceholder" />
</asp:View>
- <asp:View runat="server">
- <h2>Twitter setup</h2>
- <p>A Twitter client app must be endorsed by a Twitter user. <a target="_blank" href="http://twitter.com/oauth_clients">
- Visit Twitter and create a client app</a>. Then come back here modify your web.config
- file to include your consumer key and consumer secret. Then this page will light
- up. </p>
- </asp:View>
</asp:MultiView>
</asp:Content>
diff --git a/samples/OAuthConsumer/Twitter.aspx.cs b/samples/OAuthConsumer/Twitter.aspx.cs
index 550ed4a..a4fb0cb 100644
--- a/samples/OAuthConsumer/Twitter.aspx.cs
+++ b/samples/OAuthConsumer/Twitter.aspx.cs
@@ -13,19 +13,19 @@ using DotNetOpenAuth.OAuth;
public partial class Twitter : System.Web.UI.Page {
private string AccessToken {
- get { return (string)ViewState["AccessToken"]; }
- set { ViewState["AccessToken"] = value; }
+ get { return (string)Session["TwitterAccessToken"]; }
+ set { Session["TwitterAccessToken"] = value; }
}
private InMemoryTokenManager TokenManager {
get {
- var tokenManager = (InMemoryTokenManager)Session["TokenManager"];
+ var tokenManager = (InMemoryTokenManager)Application["TwitterTokenManager"];
if (tokenManager == null) {
string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
string consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
if (!string.IsNullOrEmpty(consumerKey)) {
tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
- Session["TokenManager"] = tokenManager;
+ Application["TwitterTokenManager"] = tokenManager;
}
}
@@ -34,33 +34,24 @@ public partial class Twitter : System.Web.UI.Page {
}
protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- if (this.TokenManager != null) {
- InMemoryTokenManager tokenManager = (InMemoryTokenManager)Session["TokenManager"];
- var twitter = new WebConsumer(TwitterConsumer.ServiceDescription, tokenManager);
+ if (this.TokenManager != null) {
+ MultiView1.ActiveViewIndex = 1;
+ if (!IsPostBack) {
+ var twitter = new WebConsumer(TwitterConsumer.ServiceDescription, this.TokenManager);
+
+ // Is Twitter calling back with authorization?
var accessTokenResponse = twitter.ProcessUserAuthorization();
if (accessTokenResponse != null) {
- // User has approved access
- MultiView1.ActiveViewIndex = 1;
-
this.AccessToken = accessTokenResponse.AccessToken;
+ } else if (this.AccessToken == null) {
+ // If we don't yet have access, immediately request it.
+ twitter.Channel.Send(twitter.PrepareRequestUserAuthorization());
}
- } else {
- MultiView1.ActiveViewIndex = 2;
}
}
}
- protected void authorizeButton_Click(object sender, EventArgs e) {
- if (!Page.IsValid) {
- return;
- }
-
- var twitter = new WebConsumer(TwitterConsumer.ServiceDescription, this.TokenManager);
- twitter.Channel.Send(twitter.PrepareRequestUserAuthorization());
- }
-
protected void downloadUpdates_Click(object sender, EventArgs e) {
var twitter = new WebConsumer(TwitterConsumer.ServiceDescription, this.TokenManager);
XPathDocument updates = new XPathDocument(TwitterConsumer.GetUpdates(twitter, AccessToken).CreateReader());
diff --git a/samples/OAuthConsumer/Web.config b/samples/OAuthConsumer/Web.config
index 4e58136..2904174 100644
--- a/samples/OAuthConsumer/Web.config
+++ b/samples/OAuthConsumer/Web.config
@@ -17,8 +17,12 @@
<appSettings>
<!-- Fill in your various consumer keys and secrets here to make the sample work. -->
<!-- You must get these values by signing up with each individual service provider. -->
+ <!-- Twitter sign-up: https://twitter.com/oauth_clients -->
<add key="twitterConsumerKey" value="" />
<add key="twitterConsumerSecret" value="" />
+ <!-- Google sign-up: https://www.google.com/accounts/ManageDomains -->
+ <add key="googleConsumerKey" value=""/>
+ <add key="googleConsumerSecret" value=""/>
</appSettings>
<connectionStrings/>
<system.web>