diff options
-rw-r--r-- | samples/OAuthConsumer/GoogleApps2Legged.aspx | 25 | ||||
-rw-r--r-- | samples/OAuthConsumer/GoogleApps2Legged.aspx.cs | 36 | ||||
-rw-r--r-- | samples/OAuthConsumer/GoogleApps2Legged.aspx.designer.cs | 42 | ||||
-rw-r--r-- | samples/OAuthConsumer/OAuthConsumer.csproj | 8 | ||||
-rw-r--r-- | src/DotNetOpenAuth/OAuth/ConsumerBase.cs | 27 |
5 files changed, 138 insertions, 0 deletions
diff --git a/samples/OAuthConsumer/GoogleApps2Legged.aspx b/samples/OAuthConsumer/GoogleApps2Legged.aspx new file mode 100644 index 0000000..cd9d9a1 --- /dev/null +++ b/samples/OAuthConsumer/GoogleApps2Legged.aspx @@ -0,0 +1,25 @@ +<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"CodeBehind="GoogleApps2Legged.aspx.cs" Inherits="OAuthConsumer.GoogleApps2Legged" %> + +<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server"> + <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> + <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"> + <h2>Updates</h2> + <p>Ok, Google has authorized us to download your contacts. Click 'Get address book' + to download the first 5 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/GoogleApps2Legged.aspx.cs b/samples/OAuthConsumer/GoogleApps2Legged.aspx.cs new file mode 100644 index 0000000..217a09c --- /dev/null +++ b/samples/OAuthConsumer/GoogleApps2Legged.aspx.cs @@ -0,0 +1,36 @@ +namespace OAuthConsumer { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Web; + using System.Web.UI; + using System.Web.UI.WebControls; + using DotNetOpenAuth.ApplicationBlock; + using System.Configuration; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.OAuth.Messages; + + public partial class GoogleApps2Legged : System.Web.UI.Page { + 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) { + var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager); + string accessToken = google.RequestNewClientAccount(); + string tokenSecret = google.TokenManager.GetTokenSecret(accessToken); + } + } +}
\ No newline at end of file diff --git a/samples/OAuthConsumer/GoogleApps2Legged.aspx.designer.cs b/samples/OAuthConsumer/GoogleApps2Legged.aspx.designer.cs new file mode 100644 index 0000000..f952937 --- /dev/null +++ b/samples/OAuthConsumer/GoogleApps2Legged.aspx.designer.cs @@ -0,0 +1,42 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace OAuthConsumer { + + + public partial class GoogleApps2Legged { + + /// <summary> + /// MultiView1 control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.MultiView MultiView1; + + /// <summary> + /// getAddressBookButton control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Button getAddressBookButton; + + /// <summary> + /// resultsPlaceholder control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.PlaceHolder resultsPlaceholder; + } +} diff --git a/samples/OAuthConsumer/OAuthConsumer.csproj b/samples/OAuthConsumer/OAuthConsumer.csproj index 284b8e9..8e80ad4 100644 --- a/samples/OAuthConsumer/OAuthConsumer.csproj +++ b/samples/OAuthConsumer/OAuthConsumer.csproj @@ -61,6 +61,7 @@ <Content Include="favicon.ico" /> <Content Include="Global.asax" /> <Content Include="GoogleAddressBook.aspx" /> + <Content Include="GoogleApps2Legged.aspx" /> <Content Include="images\Sign-in-with-Twitter-darker.png" /> <Content Include="SampleWcf.aspx" /> <Content Include="Yammer.aspx" /> @@ -92,6 +93,13 @@ <Compile Include="GoogleAddressBook.aspx.designer.cs"> <DependentUpon>GoogleAddressBook.aspx</DependentUpon> </Compile> + <Compile Include="GoogleApps2Legged.aspx.cs"> + <DependentUpon>GoogleApps2Legged.aspx</DependentUpon> + <SubType>ASPXCodeBehind</SubType> + </Compile> + <Compile Include="GoogleApps2Legged.aspx.designer.cs"> + <DependentUpon>GoogleApps2Legged.aspx</DependentUpon> + </Compile> <Compile Include="SampleWcf.aspx.cs"> <DependentUpon>SampleWcf.aspx</DependentUpon> <SubType>ASPXCodeBehind</SubType> diff --git a/src/DotNetOpenAuth/OAuth/ConsumerBase.cs b/src/DotNetOpenAuth/OAuth/ConsumerBase.cs index 2af6988..d9fa889 100644 --- a/src/DotNetOpenAuth/OAuth/ConsumerBase.cs +++ b/src/DotNetOpenAuth/OAuth/ConsumerBase.cs @@ -76,6 +76,33 @@ namespace DotNetOpenAuth.OAuth { internal OAuthChannel OAuthChannel { get; set; } /// <summary> + /// Obtains an access token for a new account at the Service Provider via 2-legged OAuth. + /// </summary> + /// <param name="requestParameters">Any applicable parameters to include in the query string of the token request.</param> + /// <returns>The access token.</returns> + /// <remarks> + /// The token secret is stored in the <see cref="TokenManager"/>. + /// </remarks> + public string RequestNewClientAccount(IDictionary<string, string> requestParameters = null) { + // Obtain an unauthorized request token. Assume the OAuth version given in the service description. + var token = new UnauthorizedTokenRequest(this.ServiceProvider.RequestTokenEndpoint, this.ServiceProvider.Version) { + ConsumerKey = this.ConsumerKey, + }; + var tokenAccessor = this.Channel.MessageDescriptions.GetAccessor(token); + tokenAccessor.AddExtraParameters(requestParameters); + var requestTokenResponse = this.Channel.Request<UnauthorizedTokenResponse>(token); + this.TokenManager.StoreNewRequestToken(token, requestTokenResponse); + + var requestAccess = new AuthorizedTokenRequest(this.ServiceProvider.AccessTokenEndpoint, this.ServiceProvider.Version) { + RequestToken = requestTokenResponse.RequestToken, + ConsumerKey = this.ConsumerKey, + }; + var grantAccess = this.Channel.Request<AuthorizedTokenResponse>(requestAccess); + this.TokenManager.ExpireRequestTokenAndStoreNewAccessToken(this.ConsumerKey, requestTokenResponse.RequestToken, grantAccess.AccessToken, grantAccess.TokenSecret); + return grantAccess.AccessToken; + } + + /// <summary> /// Creates a web request prepared with OAuth authorization /// that may be further tailored by adding parameters by the caller. /// </summary> |