diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-01-23 21:50:40 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-01-23 21:50:40 -0800 |
commit | 98976665f90b20e4757e932ce3a33268f7e1daa6 (patch) | |
tree | eeefb570e357a608dbe8a20ab1a033233cc65a7a /samples/OAuthClient/SampleWcf2.aspx.cs | |
parent | b16591e45dd70602b1899d760200e72fd6d5eaec (diff) | |
download | DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.zip DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.tar.gz DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.tar.bz2 |
Fixed a bunch more samples.
Diffstat (limited to 'samples/OAuthClient/SampleWcf2.aspx.cs')
-rw-r--r-- | samples/OAuthClient/SampleWcf2.aspx.cs | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/samples/OAuthClient/SampleWcf2.aspx.cs b/samples/OAuthClient/SampleWcf2.aspx.cs index 06bbe9b..b45434e 100644 --- a/samples/OAuthClient/SampleWcf2.aspx.cs +++ b/samples/OAuthClient/SampleWcf2.aspx.cs @@ -7,13 +7,16 @@ 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.
@@ -48,10 +51,10 @@ set { HttpContext.Current.Session["Authorization"] = value; }
}
- protected void Page_Load(object sender, EventArgs e) {
+ 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 = Client.ProcessUserAuthorization();
+ 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;
@@ -74,17 +77,18 @@ this.getNameButton.Enabled = this.getAgeButton.Enabled = this.getFavoriteSites.Enabled = Authorization != null;
}
- protected void getAuthorizationButton_Click(object sender, EventArgs e) {
+ 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();
- Client.RequestUserAuthorization(scopes);
+ var request = await Client.PrepareRequestUserAuthorizationAsync(scopes, cancellationToken: Response.ClientDisconnectedToken);
+ await request.SendAsync();
}
- protected void getNameButton_Click(object sender, EventArgs e) {
+ protected async void getNameButton_Click(object sender, EventArgs e) {
try {
- this.nameLabel.Text = this.CallService(client => client.GetName());
+ this.nameLabel.Text = await this.CallServiceAsync(client => client.GetName(), Response.ClientDisconnectedToken);
} catch (SecurityAccessDeniedException) {
this.nameLabel.Text = "Access denied!";
} catch (MessageSecurityException) {
@@ -92,9 +96,9 @@ }
}
- protected void getAgeButton_Click(object sender, EventArgs e) {
+ protected async void getAgeButton_Click(object sender, EventArgs e) {
try {
- int? age = this.CallService(client => client.GetAge());
+ 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!";
@@ -103,9 +107,9 @@ }
}
- protected void getFavoriteSites_Click(object sender, EventArgs e) {
+ protected async void getFavoriteSites_Click(object sender, EventArgs e) {
try {
- string[] favoriteSites = this.CallService(client => client.GetFavoriteSites());
+ string[] favoriteSites = await this.CallServiceAsync(client => client.GetFavoriteSites(), Response.ClientDisconnectedToken);
this.favoriteSitesLabel.Text = string.Join(", ", favoriteSites);
} catch (SecurityAccessDeniedException) {
this.favoriteSitesLabel.Text = "Access denied!";
@@ -114,7 +118,7 @@ }
}
- private T CallService<T>(Func<DataApiClient, T> predicate) {
+ private async Task<T> CallServiceAsync<T>(Func<DataApiClient, T> predicate, CancellationToken cancellationToken) {
if (Authorization == null) {
throw new InvalidOperationException("No access token!");
}
@@ -123,7 +127,7 @@ // Refresh the access token if it expires and if its lifetime is too short to be of use.
if (Authorization.AccessTokenExpirationUtc.HasValue) {
- if (Client.RefreshAuthorization(Authorization, TimeSpan.FromSeconds(30))) {
+ 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));
}
|