OpenID: Trying to Get Email Address from Google OP
Asked Answered
I

2

35

I’m using dotnetopenauth 3.2 to implement Openid and can’t figure out how to get Google to pass the email address in the Claims Response. I know that Google doesn’t support simple registration, but I can’t determine what they do support.

Caveat to this question is that I just started learning OpenID and I know I don’t have a solid grasp on the specification which I think is leading to my confusion.

Any help would be appreciated!

Impasse answered 19/8, 2009 at 16:47 Comment(0)
I
52

Ok figured it out. I posted a question on Goolge's Federated Log API group and was told to use Attribute exchange.

Below is the code for DotNetOpenAuth.

Please don't use this code in production. This is for illustration purposes only!

The Request:

using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
{
    IAuthenticationRequest request = openid.CreateRequest(openidurl);

    var fetch = new FetchRequest();
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    request.AddExtension(fetch);

    // Send your visitor to their Provider for authentication.
    request.RedirectToProvider();
}

The Response:

OpenIdRelyingParty openid = new OpenIdRelyingParty();
var response = openid.GetResponse();
if (response != null)
{
    switch (response.Status)
    {
        case AuthenticationStatus.Authenticated:
        {
            var fetch = response.GetExtension<FetchResponse>();
            string email = string.Empty();
            if (fetch != null)
            {
                email =  fetch.GetAttributeValue(
                    WellKnownAttributes.Contact.Email);
            }

            FormsAuthentication.RedirectFromLoginPage(
                response.ClaimedIdentifier, false);
            break;
        }
        ...
    }
}
Impasse answered 21/8, 2009 at 17:32 Comment(8)
this is great. Would you happen to know how to get the provider name as well (aside from analyzing the response)?Authenticity
Do you mean the claimed ID? case AuthenticationStatus.Authenticated: { string identifier = response.ClaimedIdentifier; }Tachycardia
I think he was talking about the actual friendly name of the provider ie: "Google" "Facebook" w/out having to parse the response.Giagiacamo
With the help of the above code you can also get user first name and last name - thanks zaffiroContact
fetch is always null for me, any idea why/Arenaceous
@ShawnMclean it depends on the provider and whether they support the request. Try var claimsResponse = response.GetExtension<ClaimsResponse>(); as well.Solothurn
And how would u add optional attributes those are not required..?Footman
Oh I found it.. AddOptionalFootman
P
1

When I try to get Full Name the response is null,please provide a solution to get the Full name, This post is really help ful Thank. My sample code like this.

var fetch = new FetchRequest();
            fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
            fetch.Attributes.AddRequired(WellKnownAttributes.Company.CompanyName);
            //fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);

            request.AddExtension(fetch);

And

if (fetch != null)
         {
             email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
             name = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
             company = fetch.GetAttributeValue(WellKnownAttributes.Company.CompanyName);
         } 
Pantisocracy answered 1/12, 2012 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.