How do I extract data from the DotNetOpenID AX attribute?
Andrew Arnott has a post here on how to fetch attribute exchange data from an OpenId proxy. Here is a code snippet: -
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 am getting compilation error. Basically, this doesn't exist. This is the only (readable: correct) way to do it like this ...
fetch.Attribue[WellKnownAttributes.Contact.Email].Values
cheers :)
a source to share
I'm afraid my blog post was written for DotNetOpenId 2.x, but DotNetOpenAuth 3.x has a slightly different API for extending AX and what you're working with.
What you came to is close, but not quite what you need. You will have generation NullReferenceException
or KeyNotFoundException
, if the attribute is not included in the response from the Provider. This could actually be a bug in my blog post, unless DNOI 2.x was implemented differently, I don't remember.
Anyway, here's what you need to do to hunt down the 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 it seems time consuming, just pulling out the email address, point it out to the complexity and flexibility of the AX extension itself. Sorry.
a source to share