diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2010-04-19 07:09:32 -0400 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2010-04-19 07:09:32 -0400 |
commit | 668c50fd19dc633bd367ac8ace37df8b5f9a5881 (patch) | |
tree | 8ab71b553fa22e12aae6817b3c6fab93acdd31df /samples/OAuthConsumer/SampleWcf.aspx.cs | |
parent | fef932af78eac2b775452c4a851e84a813027548 (diff) | |
download | DotNetOpenAuth-668c50fd19dc633bd367ac8ace37df8b5f9a5881.zip DotNetOpenAuth-668c50fd19dc633bd367ac8ace37df8b5f9a5881.tar.gz DotNetOpenAuth-668c50fd19dc633bd367ac8ace37df8b5f9a5881.tar.bz2 |
Updated OAuth C and SP samples to use standard WAP namespaces.
Diffstat (limited to 'samples/OAuthConsumer/SampleWcf.aspx.cs')
-rw-r--r-- | samples/OAuthConsumer/SampleWcf.aspx.cs | 194 |
1 files changed, 98 insertions, 96 deletions
diff --git a/samples/OAuthConsumer/SampleWcf.aspx.cs b/samples/OAuthConsumer/SampleWcf.aspx.cs index 25e8f55..4055c6c 100644 --- a/samples/OAuthConsumer/SampleWcf.aspx.cs +++ b/samples/OAuthConsumer/SampleWcf.aspx.cs @@ -1,117 +1,119 @@ -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.Web.UI.WebControls; -using DotNetOpenAuth; -using DotNetOpenAuth.ApplicationBlock; -using DotNetOpenAuth.Messaging; -using DotNetOpenAuth.OAuth; -using DotNetOpenAuth.OAuth.ChannelElements; -using OAuthConsumer.SampleServiceProvider; +namespace OAuthConsumer { + 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.Web.UI.WebControls; + using DotNetOpenAuth; + using DotNetOpenAuth.ApplicationBlock; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.OAuth.ChannelElements; + using OAuthConsumer.SampleServiceProvider; -/// <summary> -/// Sample consumer of our Service Provider sample's WCF service. -/// </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; - authorizationLabel.Text = "Authorized! Access token: " + accessTokenMessage.AccessToken; + /// <summary> + /// Sample consumer of our Service Provider sample's WCF service. + /// </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; + 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 scopeList.Items.OfType<ListItem>() - where item.Selected - select item.Value).ToArray(); - string scope = string.Join("|", scopes); - var requestParams = new Dictionary<string, string> { + 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 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); - } - - protected void getNameButton_Click(object sender, EventArgs e) { - try { - nameLabel.Text = CallService(client => client.GetName()); - } catch (SecurityAccessDeniedException) { - nameLabel.Text = "Access denied!"; + var response = consumer.PrepareRequestUserAuthorization(callback.Uri, requestParams, null); + consumer.Channel.Send(response); } - } - protected void getAgeButton_Click(object sender, EventArgs e) { - try { - int? age = CallService(client => client.GetAge()); - ageLabel.Text = age.HasValue ? age.Value.ToString(CultureInfo.CurrentCulture) : "not available"; - } catch (SecurityAccessDeniedException) { - ageLabel.Text = "Access denied!"; + protected void getNameButton_Click(object sender, EventArgs e) { + try { + nameLabel.Text = CallService(client => client.GetName()); + } catch (SecurityAccessDeniedException) { + nameLabel.Text = "Access denied!"; + } } - } - protected void getFavoriteSites_Click(object sender, EventArgs e) { - try { - string[] favoriteSites = CallService(client => client.GetFavoriteSites()); - favoriteSitesLabel.Text = string.Join(", ", favoriteSites); - } catch (SecurityAccessDeniedException) { - favoriteSitesLabel.Text = "Access denied!"; + protected void getAgeButton_Click(object sender, EventArgs e) { + try { + int? age = CallService(client => client.GetAge()); + ageLabel.Text = age.HasValue ? age.Value.ToString(CultureInfo.CurrentCulture) : "not available"; + } catch (SecurityAccessDeniedException) { + ageLabel.Text = "Access denied!"; + } } - } - private T CallService<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) { - throw new InvalidOperationException("No access token!"); + protected void getFavoriteSites_Click(object sender, EventArgs e) { + try { + string[] favoriteSites = CallService(client => client.GetFavoriteSites()); + favoriteSitesLabel.Text = string.Join(", ", favoriteSites); + } catch (SecurityAccessDeniedException) { + favoriteSitesLabel.Text = "Access denied!"; + } } - WebConsumer consumer = this.CreateConsumer(); - WebRequest httpRequest = consumer.PrepareAuthorizedRequest(serviceEndpoint, accessToken); - HttpRequestMessageProperty httpDetails = new HttpRequestMessageProperty(); - httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization]; - using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { - OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpDetails; - return predicate(client); - } - } + private T CallService<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) { + throw new InvalidOperationException("No access token!"); + } + WebConsumer consumer = this.CreateConsumer(); + WebRequest httpRequest = consumer.PrepareAuthorizedRequest(serviceEndpoint, accessToken); - private WebConsumer CreateConsumer() { - string consumerKey = "sampleconsumer"; - string consumerSecret = "samplesecret"; - var tokenManager = Session["WcfTokenManager"] as InMemoryTokenManager; - if (tokenManager == null) { - tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret); - Session["WcfTokenManager"] = tokenManager; + HttpRequestMessageProperty httpDetails = new HttpRequestMessageProperty(); + httpDetails.Headers[HttpRequestHeader.Authorization] = httpRequest.Headers[HttpRequestHeader.Authorization]; + using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) { + OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpDetails; + return predicate(client); + } } - 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[] { + + private WebConsumer 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); + }, + tokenManager); - return consumer; + return consumer; + } } -} +}
\ No newline at end of file |