Pure.Krome
Pure.Krome

Reputation: 86967

How do i extract data from an DotNetOpenID AX attribute?

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 :)

Upvotes: 1

Views: 541

Answers (1)

Andrew Arnott
Andrew Arnott

Reputation: 81801

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.

Upvotes: 1

Related Questions