diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-02-10 21:31:06 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-02-10 21:31:06 -0800 |
commit | 938a79f28b0fb5718cc58f8f20be5d4207bc189f (patch) | |
tree | 46ba83af2172889824c42e1ef452d8cfd2789b16 /samples/OAuthClient | |
parent | 8d530aa91c05b14be12c5b1177f39eb5f62c669e (diff) | |
download | DotNetOpenAuth-938a79f28b0fb5718cc58f8f20be5d4207bc189f.zip DotNetOpenAuth-938a79f28b0fb5718cc58f8f20be5d4207bc189f.tar.gz DotNetOpenAuth-938a79f28b0fb5718cc58f8f20be5d4207bc189f.tar.bz2 |
C# compiler warning fixes.
Diffstat (limited to 'samples/OAuthClient')
-rw-r--r-- | samples/OAuthClient/SampleWcf2.aspx.cs | 291 | ||||
-rw-r--r-- | samples/OAuthClient/SignInWithTwitter.aspx.cs | 3 | ||||
-rw-r--r-- | samples/OAuthClient/WindowsLive.aspx.cs | 3 |
3 files changed, 147 insertions, 150 deletions
diff --git a/samples/OAuthClient/SampleWcf2.aspx.cs b/samples/OAuthClient/SampleWcf2.aspx.cs index b45434e..e1b778b 100644 --- a/samples/OAuthClient/SampleWcf2.aspx.cs +++ b/samples/OAuthClient/SampleWcf2.aspx.cs @@ -1,147 +1,146 @@ -namespace OAuthClient {
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Net;
- using System.ServiceModel;
- using System.ServiceModel.Channels;
- using System.ServiceModel.Security;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using DotNetOpenAuth.OAuth2;
- using SampleResourceServer;
-
- using DotNetOpenAuth.Messaging;
-
- public partial class SampleWcf2 : System.Web.UI.Page {
- /// <summary>
- /// The OAuth 2.0 client object to use to obtain authorization and authorize outgoing HTTP requests.
- /// </summary>
- private static readonly WebServerClient Client;
-
- /// <summary>
- /// The details about the sample OAuth-enabled WCF service that this sample client calls into.
- /// </summary>
- private static AuthorizationServerDescription authServerDescription = new AuthorizationServerDescription {
- TokenEndpoint = new Uri("http://localhost:50172/OAuth/Token"),
- AuthorizationEndpoint = new Uri("http://localhost:50172/OAuth/Authorize"),
- };
-
- /// <summary>
- /// Initializes static members of the <see cref="SampleWcf2"/> class.
- /// </summary>
- static SampleWcf2() {
- Client = new WebServerClient(authServerDescription, "sampleconsumer", "samplesecret");
- }
-
- /// <summary>
- /// Gets or sets the authorization details for the logged in user.
- /// </summary>
- /// <value>The authorization details.</value>
- /// <remarks>
- /// Because this is a sample, we simply store the authorization information in memory with the user session.
- /// A real web app should store at least the access and refresh tokens in this object in a database associated with the user.
- /// </remarks>
- private static IAuthorizationState Authorization {
- get { return (AuthorizationState)HttpContext.Current.Session["Authorization"]; }
- set { HttpContext.Current.Session["Authorization"] = value; }
- }
-
- protected async void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- // Check to see if we're receiving a end user authorization response.
- var authorization = await Client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), Response.ClientDisconnectedToken);
- if (authorization != null) {
- // We are receiving an authorization response. Store it and associate it with this user.
- Authorization = authorization;
- Response.Redirect(Request.Path); // get rid of the /?code= parameter
- }
- }
-
- if (Authorization != null) {
- // Indicate to the user that we have already obtained authorization on some of these.
- foreach (var li in this.scopeList.Items.OfType<ListItem>().Where(li => Authorization.Scope.Contains(li.Value))) {
- li.Selected = true;
- }
- this.authorizationLabel.Text = "Authorization received!";
- if (Authorization.AccessTokenExpirationUtc.HasValue) {
- TimeSpan timeLeft = Authorization.AccessTokenExpirationUtc.Value - DateTime.UtcNow;
- this.authorizationLabel.Text += string.Format(CultureInfo.CurrentCulture, " (access token expires in {0} minutes)", Math.Round(timeLeft.TotalMinutes, 1));
- }
- }
-
- this.getNameButton.Enabled = this.getAgeButton.Enabled = this.getFavoriteSites.Enabled = Authorization != null;
- }
-
- protected async void getAuthorizationButton_Click(object sender, EventArgs e) {
- string[] scopes = (from item in this.scopeList.Items.OfType<ListItem>()
- where item.Selected
- select item.Value).ToArray();
-
- var request = await Client.PrepareRequestUserAuthorizationAsync(scopes, cancellationToken: Response.ClientDisconnectedToken);
- await request.SendAsync();
- }
-
- protected async void getNameButton_Click(object sender, EventArgs e) {
- try {
- this.nameLabel.Text = await this.CallServiceAsync(client => client.GetName(), Response.ClientDisconnectedToken);
- } catch (SecurityAccessDeniedException) {
- this.nameLabel.Text = "Access denied!";
- } catch (MessageSecurityException) {
- this.nameLabel.Text = "Access denied!";
- }
- }
-
- protected async void getAgeButton_Click(object sender, EventArgs e) {
- try {
- int? age = await this.CallServiceAsync(client => client.GetAge(), Response.ClientDisconnectedToken);
- this.ageLabel.Text = age.HasValue ? age.Value.ToString(CultureInfo.CurrentCulture) : "not available";
- } catch (SecurityAccessDeniedException) {
- this.ageLabel.Text = "Access denied!";
- } catch (MessageSecurityException) {
- this.ageLabel.Text = "Access denied!";
- }
- }
-
- protected async void getFavoriteSites_Click(object sender, EventArgs e) {
- try {
- string[] favoriteSites = await this.CallServiceAsync(client => client.GetFavoriteSites(), Response.ClientDisconnectedToken);
- this.favoriteSitesLabel.Text = string.Join(", ", favoriteSites);
- } catch (SecurityAccessDeniedException) {
- this.favoriteSitesLabel.Text = "Access denied!";
- } catch (MessageSecurityException) {
- this.favoriteSitesLabel.Text = "Access denied!";
- }
- }
-
- private async Task<T> CallServiceAsync<T>(Func<DataApiClient, T> predicate, CancellationToken cancellationToken) {
- if (Authorization == null) {
- throw new InvalidOperationException("No access token!");
- }
-
- var wcfClient = new DataApiClient();
-
- // Refresh the access token if it expires and if its lifetime is too short to be of use.
- if (Authorization.AccessTokenExpirationUtc.HasValue) {
- if (await Client.RefreshAuthorizationAsync(Authorization, TimeSpan.FromSeconds(30))) {
- TimeSpan timeLeft = Authorization.AccessTokenExpirationUtc.Value - DateTime.UtcNow;
- this.authorizationLabel.Text += string.Format(CultureInfo.CurrentCulture, " - just renewed for {0} more minutes)", Math.Round(timeLeft.TotalMinutes, 1));
- }
- }
-
- var httpRequest = (HttpWebRequest)WebRequest.Create(wcfClient.Endpoint.Address.Uri);
- ClientBase.AuthorizeRequest(httpRequest, Authorization.AccessToken);
-
- var httpDetails = new HttpRequestMessageProperty();
- httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization];
- using (var scope = new OperationContextScope(wcfClient.InnerChannel)) {
- OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpDetails;
- return predicate(wcfClient);
- }
- }
- }
+namespace OAuthClient { + using System; + using System.Collections.Generic; + using System.Globalization; + using System.Linq; + using System.Net; + using System.ServiceModel; + using System.ServiceModel.Channels; + using System.ServiceModel.Security; + using System.Threading; + using System.Threading.Tasks; + using System.Web; + using System.Web.UI; + using System.Web.UI.WebControls; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth2; + using SampleResourceServer; + + public partial class SampleWcf2 : System.Web.UI.Page { + /// <summary> + /// The OAuth 2.0 client object to use to obtain authorization and authorize outgoing HTTP requests. + /// </summary> + private static readonly WebServerClient Client; + + /// <summary> + /// The details about the sample OAuth-enabled WCF service that this sample client calls into. + /// </summary> + private static AuthorizationServerDescription authServerDescription = new AuthorizationServerDescription { + TokenEndpoint = new Uri("http://localhost:50172/OAuth/Token"), + AuthorizationEndpoint = new Uri("http://localhost:50172/OAuth/Authorize"), + }; + + /// <summary> + /// Initializes static members of the <see cref="SampleWcf2"/> class. + /// </summary> + static SampleWcf2() { + Client = new WebServerClient(authServerDescription, "sampleconsumer", "samplesecret"); + } + + /// <summary> + /// Gets or sets the authorization details for the logged in user. + /// </summary> + /// <value>The authorization details.</value> + /// <remarks> + /// Because this is a sample, we simply store the authorization information in memory with the user session. + /// A real web app should store at least the access and refresh tokens in this object in a database associated with the user. + /// </remarks> + private static IAuthorizationState Authorization { + get { return (AuthorizationState)HttpContext.Current.Session["Authorization"]; } + set { HttpContext.Current.Session["Authorization"] = value; } + } + + protected async void Page_Load(object sender, EventArgs e) { + if (!IsPostBack) { + // Check to see if we're receiving a end user authorization response. + var authorization = await Client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), Response.ClientDisconnectedToken); + if (authorization != null) { + // We are receiving an authorization response. Store it and associate it with this user. + Authorization = authorization; + Response.Redirect(Request.Path); // get rid of the /?code= parameter + } + } + + if (Authorization != null) { + // Indicate to the user that we have already obtained authorization on some of these. + foreach (var li in this.scopeList.Items.OfType<ListItem>().Where(li => Authorization.Scope.Contains(li.Value))) { + li.Selected = true; + } + this.authorizationLabel.Text = "Authorization received!"; + if (Authorization.AccessTokenExpirationUtc.HasValue) { + TimeSpan timeLeft = Authorization.AccessTokenExpirationUtc.Value - DateTime.UtcNow; + this.authorizationLabel.Text += string.Format(CultureInfo.CurrentCulture, " (access token expires in {0} minutes)", Math.Round(timeLeft.TotalMinutes, 1)); + } + } + + this.getNameButton.Enabled = this.getAgeButton.Enabled = this.getFavoriteSites.Enabled = Authorization != null; + } + + protected async void getAuthorizationButton_Click(object sender, EventArgs e) { + string[] scopes = (from item in this.scopeList.Items.OfType<ListItem>() + where item.Selected + select item.Value).ToArray(); + + var request = await Client.PrepareRequestUserAuthorizationAsync(scopes, cancellationToken: Response.ClientDisconnectedToken); + await request.SendAsync(); + } + + protected async void getNameButton_Click(object sender, EventArgs e) { + try { + this.nameLabel.Text = await this.CallServiceAsync(client => client.GetName(), Response.ClientDisconnectedToken); + } catch (SecurityAccessDeniedException) { + this.nameLabel.Text = "Access denied!"; + } catch (MessageSecurityException) { + this.nameLabel.Text = "Access denied!"; + } + } + + protected async void getAgeButton_Click(object sender, EventArgs e) { + try { + int? age = await this.CallServiceAsync(client => client.GetAge(), Response.ClientDisconnectedToken); + this.ageLabel.Text = age.HasValue ? age.Value.ToString(CultureInfo.CurrentCulture) : "not available"; + } catch (SecurityAccessDeniedException) { + this.ageLabel.Text = "Access denied!"; + } catch (MessageSecurityException) { + this.ageLabel.Text = "Access denied!"; + } + } + + protected async void getFavoriteSites_Click(object sender, EventArgs e) { + try { + string[] favoriteSites = await this.CallServiceAsync(client => client.GetFavoriteSites(), Response.ClientDisconnectedToken); + this.favoriteSitesLabel.Text = string.Join(", ", favoriteSites); + } catch (SecurityAccessDeniedException) { + this.favoriteSitesLabel.Text = "Access denied!"; + } catch (MessageSecurityException) { + this.favoriteSitesLabel.Text = "Access denied!"; + } + } + + private async Task<T> CallServiceAsync<T>(Func<DataApiClient, T> predicate, CancellationToken cancellationToken) { + if (Authorization == null) { + throw new InvalidOperationException("No access token!"); + } + + var wcfClient = new DataApiClient(); + + // Refresh the access token if it expires and if its lifetime is too short to be of use. + if (Authorization.AccessTokenExpirationUtc.HasValue) { + if (await Client.RefreshAuthorizationAsync(Authorization, TimeSpan.FromSeconds(30))) { + TimeSpan timeLeft = Authorization.AccessTokenExpirationUtc.Value - DateTime.UtcNow; + this.authorizationLabel.Text += string.Format(CultureInfo.CurrentCulture, " - just renewed for {0} more minutes)", Math.Round(timeLeft.TotalMinutes, 1)); + } + } + + var httpRequest = (HttpWebRequest)WebRequest.Create(wcfClient.Endpoint.Address.Uri); + ClientBase.AuthorizeRequest(httpRequest, Authorization.AccessToken); + + var httpDetails = new HttpRequestMessageProperty(); + httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization]; + using (var scope = new OperationContextScope(wcfClient.InnerChannel)) { + OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpDetails; + return predicate(wcfClient); + } + } + } }
\ No newline at end of file diff --git a/samples/OAuthClient/SignInWithTwitter.aspx.cs b/samples/OAuthClient/SignInWithTwitter.aspx.cs index bd6163a..dfc3f7a 100644 --- a/samples/OAuthClient/SignInWithTwitter.aspx.cs +++ b/samples/OAuthClient/SignInWithTwitter.aspx.cs @@ -10,9 +10,8 @@ using System.Xml.Linq; using System.Xml.XPath; using DotNetOpenAuth.ApplicationBlock; - using DotNetOpenAuth.OAuth; - using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; public partial class SignInWithTwitter : System.Web.UI.Page { protected async void Page_Load(object sender, EventArgs e) { diff --git a/samples/OAuthClient/WindowsLive.aspx.cs b/samples/OAuthClient/WindowsLive.aspx.cs index 7a3a707..b9e094a 100644 --- a/samples/OAuthClient/WindowsLive.aspx.cs +++ b/samples/OAuthClient/WindowsLive.aspx.cs @@ -9,9 +9,8 @@ using System.Web.UI.WebControls; using DotNetOpenAuth.ApplicationBlock; using DotNetOpenAuth.ApplicationBlock.Facebook; - using DotNetOpenAuth.OAuth2; - using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth2; public partial class WindowsLive : System.Web.UI.Page { private static readonly WindowsLiveClient client = new WindowsLiveClient { |