1

Andrew Arnott 有一篇关于如何从 OpenId 提供者中提取属性交换扩展数据的帖子。这是代码片段:-

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

干杯:)

4

1 回答 1

1

恐怕我的博客文章是为 DotNetOpenId 2.x 编写的,但 DotNetOpenAuth 3.x 的 AX 扩展 API 略有不同,这就是您遇到的问题。

你来的很接近,但不是你应该拥有的。如果该属性未包含在提供者的响应中,您所拥有的将生成一个NullReferenceException或。KeyNotFoundException实际上,这也可能是我的博客文章中的一个错误,除非 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 扩展本身的复杂性和灵活性。对于那个很抱歉。

于 2009-05-21T03:25:58.640 回答