diff options
Diffstat (limited to 'samples/OAuthConsumer/SampleWcf.aspx.cs')
-rw-r--r-- | samples/OAuthConsumer/SampleWcf.aspx.cs | 137 |
1 files changed, 76 insertions, 61 deletions
diff --git a/samples/OAuthConsumer/SampleWcf.aspx.cs b/samples/OAuthConsumer/SampleWcf.aspx.cs index d56a161..764b4d7 100644 --- a/samples/OAuthConsumer/SampleWcf.aspx.cs +++ b/samples/OAuthConsumer/SampleWcf.aspx.cs @@ -4,9 +4,13 @@ using System.Globalization; using System.Linq; using System.Net; + using System.Net.Http; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Security; + using System.Threading.Tasks; + using System.Web; + using System.Web.UI; using System.Web.UI.WebControls; using DotNetOpenAuth; using DotNetOpenAuth.ApplicationBlock; @@ -20,98 +24,109 @@ /// </summary> public partial class SampleWcf : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { - if (!IsPostBack) { - if (Session["WcfTokenManager"] != null) { - WebConsumer consumer = this.CreateConsumer(); - var accessTokenMessage = consumer.ProcessUserAuthorization(); - if (accessTokenMessage != null) { - Session["WcfAccessToken"] = accessTokenMessage.AccessToken; - this.authorizationLabel.Text = "Authorized! Access token: " + accessTokenMessage.AccessToken; - } - } - } + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + if (!IsPostBack) { + var consumer = this.CreateConsumer(); + if (consumer.ConsumerKey != null) { + var accessTokenMessage = await consumer.ProcessUserAuthorizationAsync(this.Request.Url); + if (accessTokenMessage != null) { + Session["WcfAccessToken"] = accessTokenMessage.AccessToken; + this.authorizationLabel.Text = "Authorized! Access token: " + accessTokenMessage.AccessToken; + } + } + } + })); } protected void getAuthorizationButton_Click(object sender, EventArgs e) { - WebConsumer consumer = this.CreateConsumer(); - UriBuilder callback = new UriBuilder(Request.Url); - callback.Query = null; - string[] scopes = (from item in this.scopeList.Items.OfType<ListItem>() - where item.Selected - select item.Value).ToArray(); - string scope = string.Join("|", scopes); - var requestParams = new Dictionary<string, string> { - { "scope", scope }, - }; - var response = consumer.PrepareRequestUserAuthorization(callback.Uri, requestParams, null); - consumer.Channel.Send(response); + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + var consumer = this.CreateConsumer(); + UriBuilder callback = new UriBuilder(Request.Url); + callback.Query = null; + string[] scopes = + (from item in this.scopeList.Items.OfType<ListItem>() where item.Selected select item.Value).ToArray(); + string scope = string.Join("|", scopes); + var requestParams = new Dictionary<string, string> { { "scope", scope }, }; + Uri redirectUri = await consumer.RequestUserAuthorizationAsync(callback.Uri, requestParams); + this.Response.Redirect(redirectUri.AbsoluteUri); + })); } protected void getNameButton_Click(object sender, EventArgs e) { - try { - this.nameLabel.Text = this.CallService(client => client.GetName()); - } catch (SecurityAccessDeniedException) { - this.nameLabel.Text = "Access denied!"; - } + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + try { + this.nameLabel.Text = await this.CallServiceAsync(client => client.GetName()); + } catch (SecurityAccessDeniedException) { + this.nameLabel.Text = "Access denied!"; + } + })); } protected void getAgeButton_Click(object sender, EventArgs e) { - try { - int? age = this.CallService(client => client.GetAge()); - this.ageLabel.Text = age.HasValue ? age.Value.ToString(CultureInfo.CurrentCulture) : "not available"; - } catch (SecurityAccessDeniedException) { - this.ageLabel.Text = "Access denied!"; - } + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + try { + int? age = await this.CallServiceAsync(client => client.GetAge()); + this.ageLabel.Text = age.HasValue ? age.Value.ToString(CultureInfo.CurrentCulture) : "not available"; + } catch (SecurityAccessDeniedException) { + this.ageLabel.Text = "Access denied!"; + } + })); } protected void getFavoriteSites_Click(object sender, EventArgs e) { - try { - string[] favoriteSites = this.CallService(client => client.GetFavoriteSites()); - this.favoriteSitesLabel.Text = string.Join(", ", favoriteSites); - } catch (SecurityAccessDeniedException) { - this.favoriteSitesLabel.Text = "Access denied!"; - } + this.RegisterAsyncTask( + new PageAsyncTask( + async ct => { + try { + string[] favoriteSites = await this.CallServiceAsync(client => client.GetFavoriteSites()); + this.favoriteSitesLabel.Text = string.Join(", ", favoriteSites); + } catch (SecurityAccessDeniedException) { + this.favoriteSitesLabel.Text = "Access denied!"; + } + })); } - private T CallService<T>(Func<DataApiClient, T> predicate) { + private async Task<T> CallServiceAsync<T>(Func<DataApiClient, T> predicate) { DataApiClient client = new DataApiClient(); var serviceEndpoint = new MessageReceivingEndpoint(client.Endpoint.Address.Uri, HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest); - var accessToken = Session["WcfAccessToken"] as string; - if (accessToken == null) { + var accessToken = (AccessToken)(Session["WcfAccessToken"] ?? default(AccessToken)); + if (accessToken.Token == null) { throw new InvalidOperationException("No access token!"); } - WebConsumer consumer = this.CreateConsumer(); - WebRequest httpRequest = consumer.PrepareAuthorizedRequest(serviceEndpoint, accessToken); + + var httpRequest = new HttpRequestMessage(HttpMethod.Post, client.Endpoint.Address.Uri); + var consumer = this.CreateConsumer(); + using (var handler = consumer.CreateMessageHandler(accessToken)) { + handler.ApplyAuthorization(httpRequest); + } HttpRequestMessageProperty httpDetails = new HttpRequestMessageProperty(); - httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization]; + httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers.Authorization.ToString(); using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpDetails; return predicate(client); } } - private WebConsumer CreateConsumer() { + private Consumer CreateConsumer() { string consumerKey = "sampleconsumer"; string consumerSecret = "samplesecret"; - var tokenManager = Session["WcfTokenManager"] as InMemoryTokenManager; - if (tokenManager == null) { - tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret); - Session["WcfTokenManager"] = tokenManager; - } MessageReceivingEndpoint oauthEndpoint = new MessageReceivingEndpoint( new Uri("http://localhost:65169/OAuth.ashx"), HttpDeliveryMethods.PostRequest); - WebConsumer consumer = new WebConsumer( - new ServiceProviderDescription { - RequestTokenEndpoint = oauthEndpoint, - UserAuthorizationEndpoint = oauthEndpoint, - AccessTokenEndpoint = oauthEndpoint, - TamperProtectionElements = new DotNetOpenAuth.Messaging.ITamperProtectionChannelBindingElement[] { - new HmacSha1SigningBindingElement(), - }, - }, - tokenManager); + var consumer = new Consumer( + consumerKey, + consumerSecret, + new ServiceProviderDescription(oauthEndpoint.Location.AbsoluteUri, oauthEndpoint.Location.AbsoluteUri, oauthEndpoint.Location.AbsoluteUri), + new CookieTemporaryCredentialStorage()); return consumer; } |