diff options
Diffstat (limited to 'samples/OAuthConsumer')
-rw-r--r-- | samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs | 6 | ||||
-rw-r--r-- | samples/OAuthConsumer/Default.aspx | 8 | ||||
-rw-r--r-- | samples/OAuthConsumer/GoogleAddressBook.aspx | 57 | ||||
-rw-r--r-- | samples/OAuthConsumer/GoogleAddressBook.aspx.cs | 80 | ||||
-rw-r--r-- | samples/OAuthConsumer/SampleWcf.aspx.cs | 4 | ||||
-rw-r--r-- | samples/OAuthConsumer/Twitter.aspx | 26 | ||||
-rw-r--r-- | samples/OAuthConsumer/Twitter.aspx.cs | 78 | ||||
-rw-r--r-- | samples/OAuthConsumer/Web.config | 20 |
8 files changed, 198 insertions, 81 deletions
diff --git a/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs b/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs index f36a396..fede300 100644 --- a/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs +++ b/samples/OAuthConsumer/App_Code/InMemoryTokenManager.cs @@ -10,10 +10,14 @@ using System.Diagnostics; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.OAuth.Messages; -public class InMemoryTokenManager : ITokenManager { +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/Default.aspx b/samples/OAuthConsumer/Default.aspx index 20e0f94..aa4ef79 100644 --- a/samples/OAuthConsumer/Default.aspx +++ b/samples/OAuthConsumer/Default.aspx @@ -1,11 +1,13 @@ -<%@ Page Title="DotNetOpenAuth Consumer samples" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" %> +<%@ Page Title="DotNetOpenAuth Consumer samples" Language="C#" MasterPageFile="~/MasterPage.master" + AutoEventWireup="true" %> <asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server"> - <p>OAuth allows this web site to access your private data with your authorization, - but without you having to give up your password. </p> + <p>OAuth allows this web site to access your private data with your authorization, but + without you having to give up your password. </p> <p>Select a demo:</p> <ul> <li><a href="GoogleAddressBook.aspx">Download your Gmail address book</a></li> + <li><a href="Twitter.aspx">Get your Twitter updates</a></li> <li><a href="SampleWcf.aspx">Interop with Service Provider sample using WCF w/ OAuth</a></li> </ul> </asp:Content> 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> - </td> - <td> - Don't have a Google Consumer Key? - <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 'Get address book' + 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 838b286..463d7e3 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; @@ -6,52 +7,67 @@ using System.Web.UI; using System.Web.UI.WebControls; using System.Xml.Linq; using DotNetOpenAuth.ApplicationBlock; +using DotNetOpenAuth.OAuth; /// <summary> /// 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 = GoogleConsumer.CreateWebConsumer(tokenManager, tokenManager.ConsumerKey); + 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() }); + this.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 = GoogleConsumer.CreateWebConsumer(tokenManager, consumerKeyBox.Text); - GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts); + XDocument contactsDocument = GoogleConsumer.GetContacts(google, this.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/SampleWcf.aspx.cs b/samples/OAuthConsumer/SampleWcf.aspx.cs index e733970..7572dd8 100644 --- a/samples/OAuthConsumer/SampleWcf.aspx.cs +++ b/samples/OAuthConsumer/SampleWcf.aspx.cs @@ -109,9 +109,7 @@ public partial class SampleWcf : System.Web.UI.Page { new HmacSha1SigningBindingElement(), }, }, - tokenManager) { - ConsumerKey = consumerKey, - }; + tokenManager); return consumer; } diff --git a/samples/OAuthConsumer/Twitter.aspx b/samples/OAuthConsumer/Twitter.aspx new file mode 100644 index 0000000..a659533 --- /dev/null +++ b/samples/OAuthConsumer/Twitter.aspx @@ -0,0 +1,26 @@ +<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" + CodeFile="Twitter.aspx.cs" Inherits="Twitter" %> + +<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> +</asp:Content> +<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server"> + <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> + <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> + <p>Ok, Twitter has authorized us to download your feeds. Click 'Get updates' to download + updates to this sample. Notice how we never asked you for your Twitter username + or password. </p> + <asp:Button ID="downloadUpdates" runat="server" Text="Get updates" OnClick="downloadUpdates_Click" /> + <asp:PlaceHolder runat="server" ID="resultsPlaceholder" /> + </asp:View> + </asp:MultiView> +</asp:Content> diff --git a/samples/OAuthConsumer/Twitter.aspx.cs b/samples/OAuthConsumer/Twitter.aspx.cs new file mode 100644 index 0000000..a4fb0cb --- /dev/null +++ b/samples/OAuthConsumer/Twitter.aspx.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Web; +using System.Web.UI; +using System.Web.UI.WebControls; +using System.Xml.Linq; +using System.Xml.XPath; +using DotNetOpenAuth.ApplicationBlock; +using DotNetOpenAuth.OAuth; + +public partial class Twitter : System.Web.UI.Page { + private string AccessToken { + get { return (string)Session["TwitterAccessToken"]; } + set { Session["TwitterAccessToken"] = value; } + } + + private InMemoryTokenManager TokenManager { + get { + 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); + Application["TwitterTokenManager"] = tokenManager; + } + } + + return tokenManager; + } + } + + protected void Page_Load(object sender, EventArgs e) { + 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) { + this.AccessToken = accessTokenResponse.AccessToken; + } else if (this.AccessToken == null) { + // If we don't yet have access, immediately request it. + 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()); + XPathNavigator nav = updates.CreateNavigator(); + var parsedUpdates = from status in nav.Select("/statuses/status").OfType<XPathNavigator>() + where !status.SelectSingleNode("user/protected").ValueAsBoolean + select new { + User = status.SelectSingleNode("user/name").InnerXml, + Status = status.SelectSingleNode("text").InnerXml, + }; + + StringBuilder tableBuilder = new StringBuilder(); + tableBuilder.Append("<table><tr><td>Name</td><td>Update</td></tr>"); + + foreach (var update in parsedUpdates) { + tableBuilder.AppendFormat( + "<tr><td>{0}</td><td>{1}</td></tr>", + HttpUtility.HtmlEncode(update.User), + HttpUtility.HtmlEncode(update.Status)); + } + tableBuilder.Append("</table>"); + resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() }); + } +} diff --git a/samples/OAuthConsumer/Web.config b/samples/OAuthConsumer/Web.config index 7d7f6aa..fc4c7dc 100644 --- a/samples/OAuthConsumer/Web.config +++ b/samples/OAuthConsumer/Web.config @@ -14,8 +14,22 @@ </sectionGroup> </sectionGroup> </configSections> - <appSettings/> + <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.net> + <defaultProxy enabled="true" /> + </system.net> + <system.web> <!-- Set compilation debug="true" to insert debugging @@ -141,9 +155,7 @@ enabled="false" /> <security mode="Message"> <transport clientCredentialType="Windows" proxyCredentialType="None" - realm=""> - <extendedProtectionPolicy policyEnforcement="Never" /> - </transport> + realm=""/> <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true" /> </security> |