Last active
December 16, 2015 21:19
-
-
Save mootinator/5498478 to your computer and use it in GitHub Desktop.
Attempt at a Google Apps OpenID client. Running into a difficult to debug problem where no useful info is returned as to why it fails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using Microsoft.Web.WebPages.OAuth; | |
namespace MvcOauth1 | |
{ | |
public static class AuthConfig | |
{ | |
public static void RegisterAuth() | |
{ | |
// To let users of this site log in using their accounts from other sites such as Microsoft, Facebook, and Twitter, | |
// you must update this site. For more information visit http://go.microsoft.com/fwlink/?LinkID=252166 | |
//OAuthWebSecurity.RegisterMicrosoftClient( | |
// clientId: "", | |
// clientSecret: ""); | |
//OAuthWebSecurity.RegisterTwitterClient( | |
// consumerKey: "", | |
// consumerSecret: ""); | |
//OAuthWebSecurity.RegisterFacebookClient( | |
// appId: "", | |
// appSecret: ""); | |
OAuthWebSecurity.RegisterClient(new GoogleAppsOpenIdClient("example.com"), "Google Apps Example", | |
new Dictionary<string, object>()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Specialized; | |
using DotNetOpenAuth.OpenId; | |
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange; | |
using DotNetOpenAuth.OpenId.Messages; | |
using DotNetOpenAuth.OpenId.RelyingParty; | |
using DotNetOpenAuth.AspNet.Clients; | |
using System.Collections.Generic; | |
/// <summary> | |
/// Represents Google OpenID client. | |
/// | |
/// </summary> | |
public sealed class GoogleAppsOpenIdClient : OpenIdClient | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="T:DotNetOpenAuth.AspNet.Clients.GoogleOpenIdClient"/> class. | |
/// | |
/// </summary> | |
public GoogleAppsOpenIdClient(string domain) | |
: base(domain, Identifier.Parse(string.Format("https://www.google.com/accounts/o8/site-xrds?ns=2&hd={0}", domain))) | |
{ | |
} | |
/// <summary> | |
/// Gets the extra data obtained from the response message when authentication is successful. | |
/// | |
/// </summary> | |
/// <param name="response">The response message. | |
/// </param> | |
/// <returns> | |
/// A dictionary of profile data; or null if no data is available. | |
/// </returns> | |
protected override Dictionary<string, string> GetExtraData(IAuthenticationResponse response) | |
{ | |
var extension = response.GetExtension<FetchResponse>(); | |
if (extension == null) | |
return null; | |
var dictionary = new Dictionary<string, string>(); | |
AddItemIfNotEmpty(dictionary, "email", extension.GetAttributeValue("http://axschema.org/contact/email")); | |
AddItemIfNotEmpty(dictionary, "country", extension.GetAttributeValue("http://axschema.org/contact/country/home")); | |
AddItemIfNotEmpty(dictionary, "firstName", extension.GetAttributeValue("http://axschema.org/namePerson/first")); | |
AddItemIfNotEmpty(dictionary, "lastName", extension.GetAttributeValue("http://axschema.org/namePerson/last")); | |
return dictionary; | |
} | |
/// <summary> | |
/// Called just before the authentication request is sent to service provider. | |
/// | |
/// </summary> | |
/// <param name="request">The request. | |
/// </param> | |
protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request) | |
{ | |
FetchRequest fetchRequest = new FetchRequest(); | |
AXUtilities.AddRequired((ICollection<AttributeRequest>)fetchRequest.Attributes, "http://axschema.org/contact/email"); | |
AXUtilities.AddOptional((ICollection<AttributeRequest>)fetchRequest.Attributes, "http://axschema.org/contact/country/home"); | |
AXUtilities.AddOptional((ICollection<AttributeRequest>)fetchRequest.Attributes, "http://axschema.org/namePerson/first"); | |
AXUtilities.AddOptional((ICollection<AttributeRequest>)fetchRequest.Attributes, "http://axschema.org/namePerson/last"); | |
request.AddExtension((IOpenIdMessageExtension)fetchRequest); | |
} | |
protected static void AddItemIfNotEmpty(Dictionary<string, string> dictionary, string key, string value) | |
{ | |
if (key == null) | |
{ | |
throw new ArgumentNullException("key"); | |
} | |
if (!string.IsNullOrEmpty(value)) | |
{ | |
dictionary[key] = value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment