summaryrefslogtreecommitdiffstats
path: root/samples/OAuthClient/Facebook.aspx.cs
blob: 101b7f6ba1776132df4ddb697ffaaddf49e44f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
namespace OAuthClient
{
	using System;
	using System.Configuration;
	using System.Net;
	using System.Web;
	using System.Web.UI;

	using DotNetOpenAuth.ApplicationBlock;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2;

	public partial class Facebook : System.Web.UI.Page
	{
		private static readonly FacebookClient facebookClient = new FacebookClient
		{
			ClientIdentifier = ConfigurationManager.AppSettings["facebookAppID"],
			ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["facebookAppSecret"]),
		};

		protected void Page_Load(object sender, EventArgs e) {
			this.RegisterAsyncTask(
				new PageAsyncTask(
					async ct => {
						IAuthorizationState authorization = await facebookClient.ProcessUserAuthorizationAsync(new HttpRequestWrapper(Request), ct);
						if (authorization == null) {
							// Kick off authorization request
							var request = await facebookClient.PrepareRequestUserAuthorizationAsync(cancellationToken: ct);
							await request.SendAsync(new HttpContextWrapper(Context), ct);
							this.Context.Response.End();

							// alternatively you can ask for more information
							// facebookClient.RequestUserAuthorization(scope: new[] { FacebookClient.Scopes.Email, FacebookClient.Scopes.UserBirthday });
						} else {
							IOAuth2Graph oauth2Graph = await facebookClient.GetGraphAsync(authorization, cancellationToken: ct);
							//// IOAuth2Graph oauth2Graph = facebookClient.GetGraph(authorization, new[] { FacebookGraph.Fields.Defaults, FacebookGraph.Fields.Email, FacebookGraph.Fields.Picture, FacebookGraph.Fields.Birthday });

							this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name);
						}
					}));
		}
	}
}