This repository has been archived by the owner on Mar 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 732
/
loginPlusOAuth.aspx.cs
71 lines (63 loc) · 2.93 KB
/
loginPlusOAuth.aspx.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace OpenIdRelyingPartyWebForms {
using System;
using System.Web.Security;
using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth.Messages;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;
public partial class loginPlusOAuth : System.Web.UI.Page {
private const string GoogleOPIdentifier = "https://www.google.com/accounts/o8/id";
private static readonly OpenIdRelyingParty relyingParty = new OpenIdRelyingParty();
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack && string.Equals(Request.Url.Host, "localhost", StringComparison.OrdinalIgnoreCase)) {
// Disable the button since the scenario won't work under localhost,
// and this will help encourage the user to read the the text above the button.
this.beginButton.Enabled = false;
}
IAuthenticationResponse authResponse = relyingParty.GetResponse();
if (authResponse != null) {
switch (authResponse.Status) {
case AuthenticationStatus.Authenticated:
State.FetchResponse = authResponse.GetExtension<FetchResponse>();
AuthorizedTokenResponse accessToken = Global.GoogleWebConsumer.ProcessUserAuthorization(authResponse);
if (accessToken != null) {
State.GoogleAccessToken = accessToken.AccessToken;
FormsAuthentication.SetAuthCookie(authResponse.ClaimedIdentifier, false);
Response.Redirect("~/MembersOnly/DisplayGoogleContacts.aspx");
} else {
MultiView1.SetActiveView(AuthorizationDenied);
}
break;
case AuthenticationStatus.Canceled:
case AuthenticationStatus.Failed:
default:
this.MultiView1.SetActiveView(this.AuthenticationFailed);
break;
}
}
}
protected void beginButton_Click(object sender, EventArgs e) {
this.GetGoogleRequest().RedirectToProvider();
}
private IAuthenticationRequest GetGoogleRequest() {
// Google requires that the realm and consumer key be equal,
// so we constrain the realm to match the realm in the web.config file.
// This does mean that the return_to URL must also fall under the key,
// which means this sample will only work on a public web site
// that is properly registered with Google.
// We will customize the realm to use http or https based on what the
// return_to URL will be (which will be this page).
Realm realm = Request.Url.Scheme + Uri.SchemeDelimiter + Global.GoogleTokenManager.ConsumerKey + "/";
IAuthenticationRequest authReq = relyingParty.CreateRequest(GoogleOPIdentifier, realm);
// Prepare the OAuth extension
string scope = GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Contacts);
Global.GoogleWebConsumer.AttachAuthorizationRequest(authReq, scope);
// We also want the user's email address
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
authReq.AddExtension(fetch);
return authReq;
}
}
}