Skip to content

Instantly share code, notes, and snippets.

@Buildstarted
Created January 31, 2013 03:21
Show Gist options
  • Select an option

  • Save Buildstarted/4679694 to your computer and use it in GitHub Desktop.

Select an option

Save Buildstarted/4679694 to your computer and use it in GitHub Desktop.
sample openid provider for world domination
namespace WorldDomination.Web.Authentication.OpenId
{
using System;
using System.Collections.Specialized;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;
/// <summary>
/// OpenId Provider for World Domination
/// </summary>
public class OpenIdProvider : IAuthenticationProvider
{
static readonly OpenIdRelyingParty OpenId = new OpenIdRelyingParty(new StandardRelyingPartyApplicationStore());
public string Name { get { return "OpenId"; } }
public Uri CallBackUri { get; private set; }
public IAuthenticationServiceSettings DefaultAuthenticationServiceSettings { get { return new OpenIdAuthenticationServiceSettings(); } }
public Uri RedirectToAuthenticate(IAuthenticationServiceSettings authenticationServiceSettings)
{
//as this is an alternateidentifier request
//we need to identifier they're attempting to authenticate
var settings = authenticationServiceSettings as IAlternateIdentifier;
try
{
//local server
string realm = "http://" + authenticationServiceSettings.CallBackUri.Authority;
Identifier identifier;
if (Identifier.TryParse(settings.Identifier, out identifier))
{
//create the openid request
//and add request for personal information
var request = OpenId.CreateRequest(identifier, new Realm(realm), authenticationServiceSettings.CallBackUri);
request.AddExtension(new ClaimsRequest
{
FullName = DemandLevel.Request,
Language = DemandLevel.Request,
Country = DemandLevel.Request,
Email = DemandLevel.Request,
Gender = DemandLevel.Request,
});
var response = request.RedirectingResponse;
//grab the redirect from the headers of the response
return new Uri(response.Headers["Location"]);
}
else
{
throw new ProtocolException();
}
}
catch (ProtocolException ex)
{
throw new InvalidOperationException();
}
}
public IAuthenticatedClient AuthenticateClient(NameValueCollection parameters, string existingState)
{
//use openid to parse the response into something usable
var response = OpenId.GetResponse();
//we requested personal info, attempt to retrieve that
var userInfo = GetUserInfo(response);
//return the new authenticated client
//by default the openid provider doesn't
//have a max value for the access token
//expiration. Not sure if this even exists
return new AuthenticatedClient(Name.ToLowerInvariant())
{
AccessToken = response.ClaimedIdentifier,
AccessTokenExpiresOn = DateTime.MaxValue,
UserInformation = userInfo
};
}
private UserInformation GetUserInfo(IAuthenticationResponse response)
{
var fetch = response.GetExtension<ClaimsResponse>();
if (fetch != null)
{
return new UserInformation()
{
Email = fetch.Email,
Gender = (GenderType)Enum.Parse(typeof(GenderType), fetch.Gender.GetValueOrDefault().ToString(), true),
Locale = fetch.Language,
Name = fetch.FullName,
Picture = null,
UserName = fetch.Nickname
};
}
return new UserInformation();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment