How do i extract data from an DotNetOpenID AX attribute?

*爱你&永不变心* 提交于 2019-12-24 07:58:44

问题


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:


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.



来源:https://stackoverflow.com/questions/887748/how-do-i-extract-data-from-an-dotnetopenid-ax-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!