views:

195

answers:

1

Hi folks,

Andrew Arnott has a post here about how to extract the attribute exchange extension data, from an OpenId proivder. Here's a snippet of the code :-

var fetch = openid.Response.GetExtension<FetchResponse>();   
if (fetch != null)
{   
    IList<string> emailAddresses = fetch.GetAttribute
                                   (WellKnownAttributes.Contact.Email).Values;   
    IList<string> fullNames = fetch.GetAttribute
                                   (WellKnownAttributes.Name.FullName).Values;   
    string email = emailAddresses.Count > 0 ? emailAddresses[0] : null;   
    string fullName = fullNames.Count > 0 ? fullNames[0] : null;   
}

When i try to do the following...

fetch.GetAttribute(...)

I get a compile error. Basically, that doesn't exist. Is the only (read: proper) way to do this as follows...

fetch.Attribue[WellKnownAttributes.Contact.Email].Values

cheers :)

+1  A: 

I'm afraid my blog post was written for DotNetOpenId 2.x, but DotNetOpenAuth 3.x has a slightly different API for the AX extension and that's what you're running into.

What you came to is close, but not quite what you should have. What you have would generate a NullReferenceException or KeyNotFoundException if the attribute isn't included in the response from the Provider. Actually that might be a bug in my blog post too, unless DNOI 2.x was implemented differently I don't recall.

Anyway, here's what you should do to fish out an email address:

if (fetch.Attributes.Contains(WellKnownAttributes.Contact.Email)) {
    IList<string> emailAddresses =
        fetch.Attributes[WellKnownAttributes.Contact.Email].Values;
    string email = emailAddresses.Count > 0 ? emailAddresses[0] : null;
    // do something with email
}

If that seems laborious for just pulling out the email address, chalk it up to the complexity and flexibility of the AX extension itself. Sorry about that.

Andrew Arnott
You know, seeing how cumbersome the "right" code is to fetch a single AX value motivated me to add a helper method to the FetchResponse class. It will be in v3.2.http://dotnetopenauth.net:8000/ticket/67
Andrew Arnott
Yeah. i added an extension method which did this last night. I posted it in my another SO post. But you've added it which is all good :) my extension method passed in a params of keys, so i can extract the first value for the first key, in the key params :)
Pure.Krome
Also, will emailAddresses always return an instance? could it return NULL ?
Pure.Krome
the Values property always returns non-null, so emailAddresses will be non-null as well.
Andrew Arnott