Your official information source from the .NET Web Development and Tools group at Microsoft.
In the previous post, I wrote about how you can use the existing providers for Google, Facebook etc. and retrieve extra metadata about the authenticated users. Let’s assume you wanted to change the way the providers request for information. Some examples of this could be
This post covers how you can write your own provider and plug it into your ASP.NET web application
Each Provider implements from OpenIdClient. Following example shows a custom implementation of Google Provider which requests information about the user such as firstname/lastname etc
Please Note: This addresses a bug with the existing google provider which does not return the extra data about the user such as Country/FirstName/LastName. The version of google provider is DotNetOpenAuth.AspNet" version="4.0.3.12153". We have logged a bug for this and will fix it in next update of this package.
namespace MyApplication
{
using System.Collections.Generic;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;
/// <summary>
/// Represents Google OpenID client.
/// </summary>
public class GoogleCustomClient : OpenIdClient
#region Constructors and Destructors
public GoogleCustomClient()
: base("google", WellKnownProviders.Google) { }
#endregion
#region Methods
/// Gets the extra data obtained from the response message when authentication is successful.
/// <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)
FetchResponse fetchResponse = response.GetExtension<FetchResponse>();
if (fetchResponse != null)
var extraData = new Dictionary<string, string>();
extraData.Add("email", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email));
extraData.Add("country", fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.HomeAddress.Country));
extraData.Add("firstName", fetchResponse.GetAttributeValue(WellKnownAttributes.Name.First));
extraData.Add("lastName", fetchResponse.GetAttributeValue(WellKnownAttributes.Name.Last));
return extraData;
}
return null;
/// Called just before the authentication request is sent to service provider.
/// <param name="request">
/// The request.
protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
// Attribute Exchange extensions
var fetchRequest = new FetchRequest();
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.HomeAddress.Country);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);
request.AddExtension(fetchRequest);
The source code for existing providers is public and can be accessed at https://github.com/AArnott/dotnetopenid/tree/master/src/DotNetOpenAuth.AspNet/Clients
WebForms
OpenAuth.AuthenticationClients.Add("Custom Google", () => new MyApplication.GoogleCustomClient());
//OpenAuth.AuthenticationClients.AddGoogle();
MVC
OAuthWebSecurity.RegisterClient(new MyApplication.GoogleCustomClient(),"Google",null);
// OAuthWebSecurity.RegisterGoogleClient();
WebPages
This post has been cross posted to http://blogs.msdn.com/b/pranav_rastogi/archive/2012/08/23/plugging-custom-oauth-openid-providers.aspx
Please do reach me via twitter (@rustd) for any questions
This looks so easy! I was always not a big friend of OAuth - but this is really amazing! Thank you!
Can you provide an example of how I would hook up a third party provider (i.e. something outside of Facebook, Twitter, and Google)?
@Ryan you can use the same model to hook up any third party provider such as linkedin, yahoo etc
Great post, thanks. The first I've seen which covers this. I'm keen to request some extra fields from Facebook and as the methods are different I can't work out how I'd do that and extend the scope to request email and possible write to wall permissions. Any pointers you can give me?
Thanks for the blog post. Very useful.