2009-05-20 49 views
1

Andrew Arnott在这里有一篇关于如何从OpenId proivder中提取属性交换扩展数据的文章。这里的代码片段: -我如何从DotNetOpenID AX属性提取数据?

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; 
} 

当我尝试做以下...

fetch.GetAttribute(...) 

我得到一个编译错误。基本上,这不存在。是唯一的(阅读:正确)的方式来做到这一点如下...

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

欢呼:)

回答

1

我怕我的博客文章是为DotNetOpenId 2.X写的,但DotNetOpenAuth 3。 x对于AX扩展有一个稍微不同的API,这就是你正在运行的。

你来到的是关闭,但不是你应该有的。如果该属性未包含在来自提供者的响应中,您将产生NullReferenceExceptionKeyNotFoundException。实际上,这也可能是我博客文章中的一个错误,除非DNOI 2.x的实现方式不同,我不记得。

不管怎么说,这是你应该做的鱼发了一个电子邮件地址:

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 
} 

如果这看起来费力只是拉出的电子邮件地址,粉笔它的AX扩展本身的复杂性和灵活性。对于那个很抱歉。

+0

你知道,看到“正确”的代码是多么繁琐以获取一个单一的AX值促使我添加一个辅助方法到FetchResponse类。它将在v3.2中。 http://dotnetopenauth.net:8000/ticket/67 – 2009-05-21 03:40:10