Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom OpenIdClient for Customer URL in MVC 4

I'm working with the default template for MVC 4 and trying to add my own openID provider for example http://steamcommunity.com/dev to the list of openID logins and an openID box where the user can type in their openID information.

To add Google I just un-comment

OAuthWebSecurity.RegisterGoogleClient();

as for other custom solutions you can do something like

OAuthWebSecurity.RegisterClient(new SteamClient(),"Steam",null);

The trouble I have is creating SteamClient (or a generic one) http://blogs.msdn.com/b/webdev/archive/2012/08/23/plugging-custom-oauth-openid-providers.aspx doesn't show anywhere to change the URL.

like image 214
Jeff Avatar asked Dec 15 '22 19:12

Jeff


2 Answers

I think the reason I could not find the answer is that most people thought it was common sense. I prefer my sense to be uncommon.

public class OidCustomClient : OpenIdClient
{
  public OidCustomClient() : base("Oid", "http://localhost:5004/") { }
}
like image 122
Jeff Avatar answered Jan 11 '23 23:01

Jeff


Based on @Jeff's answer I created a class to handle Stack Exchange OpenID.

Register:

OAuthWebSecurity.RegisterClient(new StackExchangeOpenID());

Class:

public class StackExchangeOpenID : OpenIdClient
{
    public StackExchangeOpenID()
        : base("stackexchange", "https://openid.stackexchange.com")
    {

    }

    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("name", fetchResponse.GetAttributeValue(WellKnownAttributes.Name.FullName));
            return extraData;
        }

        return null;
    }
    protected override void OnBeforeSendingAuthenticationRequest(IAuthenticationRequest request)
    {
        var fetchRequest = new FetchRequest();
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
        request.AddExtension(fetchRequest);
    }
}

Retrieving extra data:

var result = OAuthWebSecurity.VerifyAuthentication();
result.ExtraData["email"];
result.ExtraData["name"];
like image 20
BrunoLM Avatar answered Jan 11 '23 23:01

BrunoLM