summaryrefslogtreecommitdiffstats
path: root/samples/OAuthConsumer/SampleWcf2.aspx.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2010-06-02 22:39:46 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2010-06-02 22:39:46 -0700
commitccbec1badf616062cf9cb102ae4a0b2d835610b0 (patch)
tree11037cf8c3edbbd444bdccfc2efe6dad82027bd6 /samples/OAuthConsumer/SampleWcf2.aspx.cs
parent9db9a3768ce97f565ff01ca355be295d9775c4ef (diff)
downloadDotNetOpenAuth-ccbec1badf616062cf9cb102ae4a0b2d835610b0.zip
DotNetOpenAuth-ccbec1badf616062cf9cb102ae4a0b2d835610b0.tar.gz
DotNetOpenAuth-ccbec1badf616062cf9cb102ae4a0b2d835610b0.tar.bz2
OAuth 2.0 web flow now works, client, auth server, and resource server, in the sample!
Yay.
Diffstat (limited to 'samples/OAuthConsumer/SampleWcf2.aspx.cs')
-rw-r--r--samples/OAuthConsumer/SampleWcf2.aspx.cs62
1 files changed, 58 insertions, 4 deletions
diff --git a/samples/OAuthConsumer/SampleWcf2.aspx.cs b/samples/OAuthConsumer/SampleWcf2.aspx.cs
index 09ebd12..fa7a102 100644
--- a/samples/OAuthConsumer/SampleWcf2.aspx.cs
+++ b/samples/OAuthConsumer/SampleWcf2.aspx.cs
@@ -1,16 +1,27 @@
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;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuthWrap;
+ using OAuthConsumer.SampleServiceProvider;
public partial class SampleWcf2 : System.Web.UI.Page {
private static InMemoryClientTokenManager TokenManager = new InMemoryClientTokenManager();
+ private static IAuthorizationState Authorization {
+ get { return (AuthorizationState)HttpContext.Current.Session["Authorization"]; }
+ set { HttpContext.Current.Session["Authorization"] = value; }
+ }
+
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack)
{
@@ -18,7 +29,7 @@
var authorization = client.ProcessUserAuthorization();
if (authorization != null)
{
- Response.Write("Obtained access token: " + authorization.AccessToken);
+ Authorization = authorization;
}
}
}
@@ -27,7 +38,7 @@
string[] scopes = (from item in this.scopeList.Items.OfType<ListItem>()
where item.Selected
select item.Value).ToArray();
- string scope = string.Join("|", scopes);
+ string scope = string.Join(" ", scopes);
var client = CreateClient();
string clientState;
@@ -36,13 +47,56 @@
client.Channel.Send(response);
}
+ protected void getNameButton_Click(object sender, EventArgs e) {
+ try {
+ this.nameLabel.Text = CallService(client => client.GetName());
+ } catch (SecurityAccessDeniedException) {
+ this.nameLabel.Text = "Access denied!";
+ }
+ }
+
+ protected void getAgeButton_Click(object sender, EventArgs e) {
+ try {
+ int? age = CallService(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 = CallService(client => client.GetFavoriteSites());
+ this.favoriteSitesLabel.Text = string.Join(", ", favoriteSites);
+ } catch (SecurityAccessDeniedException) {
+ this.favoriteSitesLabel.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);
+ if (Authorization == null) {
+ throw new InvalidOperationException("No access token!");
+ }
+
+ var httpRequest = (HttpWebRequest)WebRequest.Create(client.Endpoint.Address.Uri);
+ WebAppClient.AuthorizeRequest(httpRequest, Authorization);
+
+ var 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 static WebAppClient CreateClient() {
var authServerDescription = new AuthorizationServerDescription {
TokenEndpoint = new Uri("http://localhost:65169/OAuth2.ashx/token"),
AuthorizationEndpoint = new Uri("http://localhost:65169/OAuth2.ashx/auth"),
};
- var client = new WebAppClient(authServerDescription)
- {
+ var client = new WebAppClient(authServerDescription) {
ClientIdentifier = "sampleconsumer",
ClientSecret = "samplesecret",
TokenManager = TokenManager,