From 01a2ce0ccba9d8ba9d562d10c22253a7336ccffd Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Sat, 4 Oct 2008 22:51:21 -0700 Subject: [PATCH] Added facility so WCF service knows who is calling it. --- samples/Consumer/SampleWcf.aspx.cs | 32 ++++++++++++---------- samples/ServiceProvider/App_Code/DataApi.cs | 10 +++---- .../App_Code/OAuthAuthorizationManager.cs | 6 ++-- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/samples/Consumer/SampleWcf.aspx.cs b/samples/Consumer/SampleWcf.aspx.cs index af89f61..4f7730f 100644 --- a/samples/Consumer/SampleWcf.aspx.cs +++ b/samples/Consumer/SampleWcf.aspx.cs @@ -14,7 +14,7 @@ public partial class SampleWcf : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["WcfTokenManager"] != null) { - Consumer consumer = CreateConsumer(); + Consumer consumer = this.CreateConsumer(); var accessTokenMessage = consumer.ProcessUserAuthorization(); if (accessTokenMessage != null) { Session["WcfAccessToken"] = accessTokenMessage.AccessToken; @@ -24,15 +24,15 @@ public partial class SampleWcf : System.Web.UI.Page { } protected void getAuthorizationButton_Click(object sender, EventArgs e) { - Consumer consumer = CreateConsumer(); + Consumer consumer = this.CreateConsumer(); consumer.RequestUserAuthorization().Send(); } - + protected void getNameButton_Click(object sender, EventArgs e) { DataApiClient client = new DataApiClient(); var serviceEndpoint = new MessageReceivingEndpoint(client.Endpoint.Address.Uri, HttpDeliveryMethod.AuthorizationHeaderRequest | HttpDeliveryMethod.PostRequest); var accessToken = Session["WcfAccessToken"] as string; - Consumer consumer = CreateConsumer(); + Consumer consumer = this.CreateConsumer(); WebRequest httpRequest = consumer.CreateAuthorizedRequest(serviceEndpoint, accessToken); HttpRequestMessageProperty httpDetails = new HttpRequestMessageProperty(); @@ -44,7 +44,7 @@ public partial class SampleWcf : System.Web.UI.Page { } Response.Write(name); } - + protected void getAgeButton_Click(object sender, EventArgs e) { } @@ -59,17 +59,19 @@ public partial class SampleWcf : System.Web.UI.Page { MessageReceivingEndpoint oauthEndpoint = new MessageReceivingEndpoint( new Uri("http://localhost:65169/ServiceProvider/OAuth.ashx"), HttpDeliveryMethod.PostRequest); - Consumer consumer = new Consumer(new ServiceProviderDescription { - RequestTokenEndpoint = oauthEndpoint, - UserAuthorizationEndpoint = oauthEndpoint, - AccessTokenEndpoint = oauthEndpoint, - TamperProtectionElements = new DotNetOAuth.Messaging.ITamperProtectionChannelBindingElement[] { - new HmacSha1SigningBindingElement(), + Consumer consumer = new Consumer( + new ServiceProviderDescription { + RequestTokenEndpoint = oauthEndpoint, + UserAuthorizationEndpoint = oauthEndpoint, + AccessTokenEndpoint = oauthEndpoint, + TamperProtectionElements = new DotNetOAuth.Messaging.ITamperProtectionChannelBindingElement[] { + new HmacSha1SigningBindingElement(), + }, }, - }, tokenManager) { - ConsumerKey = consumerKey, - ConsumerSecret = consumerSecret, - }; + tokenManager) { + ConsumerKey = consumerKey, + ConsumerSecret = consumerSecret, + }; return consumer; } diff --git a/samples/ServiceProvider/App_Code/DataApi.cs b/samples/ServiceProvider/App_Code/DataApi.cs index 9e679cd..1a7555c 100644 --- a/samples/ServiceProvider/App_Code/DataApi.cs +++ b/samples/ServiceProvider/App_Code/DataApi.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; +using System.Globalization; using System.ServiceModel; -using System.Text; public class DataApi : IDataApi { public int GetAge() { @@ -11,6 +7,8 @@ public class DataApi : IDataApi { } public string GetName() { - return "Andrew"; + string consumerKey = OperationContext.Current.IncomingMessageProperties["OAuthConsumerKey"] as string; + string accessToken = OperationContext.Current.IncomingMessageProperties["OAuthAccessToken"] as string; + return string.Format(CultureInfo.InvariantCulture, "Andrew_{0}_{1}", consumerKey.Substring(0, 1), accessToken.Substring(0, 1)); } } diff --git a/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs b/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs index 20536d8..a53e6b9 100644 --- a/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs +++ b/samples/ServiceProvider/App_Code/OAuthAuthorizationManager.cs @@ -20,10 +20,8 @@ public class OAuthAuthorizationManager : ServiceAuthorizationManager { ServiceProvider sp = Constants.CreateServiceProvider(); var auth = sp.GetProtectedResourceAuthorization(httpDetails, requestUri); if (auth != null) { - string consumer = auth.ConsumerKey; - - //// TODO: pass the consumer along to the operation somehow - + operationContext.IncomingMessageProperties["OAuthConsumerKey"] = auth.ConsumerKey; + operationContext.IncomingMessageProperties["OAuthAccessToken"] = auth.AccessToken; return true; } -- 2.11.4.GIT