summaryrefslogtreecommitdiffstats
path: root/samples/OAuthClient/Google.aspx.cs
blob: e0c4941e7eec648421557a550d51e55ff4894202 (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
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 Google : System.Web.UI.Page
	{
		private static readonly GoogleClient googleClient = new GoogleClient
		{
			ClientIdentifier = ConfigurationManager.AppSettings["googleClientID"],
			ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["googleClientSecret"]),
		};

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

							// alternatively you can ask for more information
							// googleClient.RequestUserAuthorization(scope: new[] { GoogleClient.Scopes.UserInfo.Profile, GoogleClient.Scopes.UserInfo.Email });
						} else {
							IOAuth2Graph oauth2Graph = await googleClient.GetGraphAsync(authorization, cancellationToken: ct);

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