如何从DotNetOpenID AX属性中提取数据?

时间:2021-09-05 00:01:54

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

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

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 个解决方案

#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.

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

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.

你得到的是接近,但不是你应该拥有的。如果属性未包含在Provider的响应中,那么您将生成NullReferenceException或KeyNotFoundException。实际上,这也可能是我博客文章中的一个错误,除非DNOI 2.x以不同的方式实现,我不记得了。

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.

如果仅仅拔出电子邮件地址似乎很费力,那就直接考虑到AX扩展本身的复杂性和灵活性。对于那个很抱歉。

#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.

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

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.

你得到的是接近,但不是你应该拥有的。如果属性未包含在Provider的响应中,那么您将生成NullReferenceException或KeyNotFoundException。实际上,这也可能是我博客文章中的一个错误,除非DNOI 2.x以不同的方式实现,我不记得了。

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.

如果仅仅拔出电子邮件地址似乎很费力,那就直接考虑到AX扩展本身的复杂性和灵活性。对于那个很抱歉。