diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-04 15:02:41 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-04 15:02:41 -0800 |
commit | bbb0214b9f41ad8bcd44eba3714b463eee29fe86 (patch) | |
tree | 30787ba8be3b39fce16dc7d32f1e6f6ce177340c /samples/OAuthClient | |
parent | 9c7bd4ad8a4de0147ad93d00813eb4874e031477 (diff) | |
download | DotNetOpenAuth-bbb0214b9f41ad8bcd44eba3714b463eee29fe86.zip DotNetOpenAuth-bbb0214b9f41ad8bcd44eba3714b463eee29fe86.tar.gz DotNetOpenAuth-bbb0214b9f41ad8bcd44eba3714b463eee29fe86.tar.bz2 |
Changed all ASP.NET async void event handlers to use RegisterAsyncTask, which avoids bugs in ASP.NET.
Diffstat (limited to 'samples/OAuthClient')
-rw-r--r-- | samples/OAuthClient/Facebook.aspx.cs | 42 | ||||
-rw-r--r-- | samples/OAuthClient/SampleWcf2.aspx.cs | 139 | ||||
-rw-r--r-- | samples/OAuthClient/WindowsLive.aspx.cs | 56 |
3 files changed, 139 insertions, 98 deletions
diff --git a/samples/OAuthClient/Facebook.aspx.cs b/samples/OAuthClient/Facebook.aspx.cs index 3a9cf14..19211bc 100644 --- a/samples/OAuthClient/Facebook.aspx.cs +++ b/samples/OAuthClient/Facebook.aspx.cs @@ -3,6 +3,8 @@ using System.Configuration; using System.Net; using System.Web; + using System.Web.UI; + using DotNetOpenAuth.ApplicationBlock; using DotNetOpenAuth.ApplicationBlock.Facebook; using DotNetOpenAuth.Messaging; @@ -14,22 +16,30 @@ ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["facebookAppSecret"]), }; - protected async void Page_Load(object sender, EventArgs e) { - IAuthorizationState authorization = await client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), Response.ClientDisconnectedToken); - if (authorization == null) { - // Kick off authorization request - var request = await client.PrepareRequestUserAuthorizationAsync(cancellationToken: Response.ClientDisconnectedToken); - await request.SendAsync(new HttpContextWrapper(Context), Response.ClientDisconnectedToken); - this.Context.Response.End(); - } else { - var request = WebRequest.Create("https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); - using (var response = request.GetResponse()) { - using (var responseStream = response.GetResponseStream()) { - var graph = FacebookGraph.Deserialize(responseStream); - this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name); - } - } - } + protected void Page_Load(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + IAuthorizationState authorization = + await client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), ct); + if (authorization == null) { + // Kick off authorization request + var request = + await client.PrepareRequestUserAuthorizationAsync(cancellationToken: ct); + await request.SendAsync(new HttpContextWrapper(Context), ct); + this.Context.Response.End(); + } else { + var request = + WebRequest.Create( + "https://graph.facebook.com/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); + using (var response = request.GetResponse()) { + using (var responseStream = response.GetResponseStream()) { + var graph = FacebookGraph.Deserialize(responseStream); + this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name); + } + } + } + })); } } }
\ No newline at end of file diff --git a/samples/OAuthClient/SampleWcf2.aspx.cs b/samples/OAuthClient/SampleWcf2.aspx.cs index af12a26..e96a5c0 100644 --- a/samples/OAuthClient/SampleWcf2.aspx.cs +++ b/samples/OAuthClient/SampleWcf2.aspx.cs @@ -50,72 +50,95 @@ 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 void Page_Load(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + 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(); - this.Context.Response.End(); + protected void getAuthorizationButton_Click(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + 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(); + this.Context.Response.End(); + })); } - 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 void getNameButton_Click(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + 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 void getAgeButton_Click(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + 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!"; - } + protected void getFavoriteSites_Click(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + 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) { diff --git a/samples/OAuthClient/WindowsLive.aspx.cs b/samples/OAuthClient/WindowsLive.aspx.cs index 23566c5..efe41ec 100644 --- a/samples/OAuthClient/WindowsLive.aspx.cs +++ b/samples/OAuthClient/WindowsLive.aspx.cs @@ -18,31 +18,39 @@ ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["WindowsLiveAppSecret"]), }; - protected async void Page_Load(object sender, EventArgs e) { - if (string.Equals("localhost", this.Request.Headers["Host"].Split(':')[0], StringComparison.OrdinalIgnoreCase)) { - this.localhostDoesNotWorkPanel.Visible = true; - var builder = new UriBuilder(this.publicLink.NavigateUrl); - builder.Port = this.Request.Url.Port; - this.publicLink.NavigateUrl = builder.Uri.AbsoluteUri; - this.publicLink.Text = builder.Uri.AbsoluteUri; - } else { - IAuthorizationState authorization = await client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), Response.ClientDisconnectedToken); - if (authorization == null) { - // Kick off authorization request - var request = await client.PrepareRequestUserAuthorizationAsync(scopes: new[] { WindowsLiveClient.Scopes.Basic }); // this scope isn't even required just to log in - await request.SendAsync(new HttpContextWrapper(this.Context), Response.ClientDisconnectedToken); - this.Context.Response.End(); - } else { - var request = - WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); - using (var response = request.GetResponse()) { - using (var responseStream = response.GetResponseStream()) { - var graph = WindowsLiveGraph.Deserialize(responseStream); - this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name); + protected void Page_Load(object sender, EventArgs e) { + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + if (string.Equals("localhost", this.Request.Headers["Host"].Split(':')[0], StringComparison.OrdinalIgnoreCase)) { + this.localhostDoesNotWorkPanel.Visible = true; + var builder = new UriBuilder(this.publicLink.NavigateUrl); + builder.Port = this.Request.Url.Port; + this.publicLink.NavigateUrl = builder.Uri.AbsoluteUri; + this.publicLink.Text = builder.Uri.AbsoluteUri; + } else { + IAuthorizationState authorization = + await client.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), Response.ClientDisconnectedToken); + if (authorization == null) { + // Kick off authorization request + var request = + await client.PrepareRequestUserAuthorizationAsync(scopes: new[] { WindowsLiveClient.Scopes.Basic }); + // this scope isn't even required just to log in + await request.SendAsync(new HttpContextWrapper(this.Context), Response.ClientDisconnectedToken); + this.Context.Response.End(); + } else { + var request = + WebRequest.Create( + "https://apis.live.net/v5.0/me?access_token=" + Uri.EscapeDataString(authorization.AccessToken)); + using (var response = request.GetResponse()) { + using (var responseStream = response.GetResponseStream()) { + var graph = WindowsLiveGraph.Deserialize(responseStream); + this.nameLabel.Text = HttpUtility.HtmlEncode(graph.Name); + } + } + } } - } - } - } + })); } } } |