OpenID: Trying to Get Email Address from Google OP

2019-06-18 14:41发布

我使用dotnetopenauth 3.2实现OpenID和无法弄清楚如何让谷歌在声明响应传递的电子邮件地址。 我知道,谷歌不支持简单的登记,但我不能确定他们做了什么支持。

需要注意这个问题是我刚开始学习的OpenID,我知道我没有关于这一点我认为是导致我的困惑规范扎实掌握。

任何帮助,将不胜感激!

Answer 1:

好了它。 我贴在一个问题的Goolge的联合登录API组 ,被告知使用属性交换 。

下面是代码DotNetOpenAuth 。

请不要在生产中使用此代码。 这是仅用于说明目的!

请求:

using (OpenIdRelyingParty openid = new OpenIdRelyingParty())
{
    IAuthenticationRequest request = openid.CreateRequest(openidurl);

    var fetch = new FetchRequest();
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    request.AddExtension(fetch);

    // Send your visitor to their Provider for authentication.
    request.RedirectToProvider();
}

响应:

OpenIdRelyingParty openid = new OpenIdRelyingParty();
var response = openid.GetResponse();
if (response != null)
{
    switch (response.Status)
    {
        case AuthenticationStatus.Authenticated:
        {
            var fetch = response.GetExtension<FetchResponse>();
            string email = string.Empty();
            if (fetch != null)
            {
                email =  fetch.GetAttributeValue(
                    WellKnownAttributes.Contact.Email);
            }

            FormsAuthentication.RedirectFromLoginPage(
                response.ClaimedIdentifier, false);
            break;
        }
        ...
    }
}


Answer 2:

当我试图让全名的响应为空,请提供一个解决方案,以获得全名,这篇文章是真正帮助FUL谢谢。 我的示例代码是这样的。

var fetch = new FetchRequest();
            fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
            fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
            fetch.Attributes.AddRequired(WellKnownAttributes.Company.CompanyName);
            //fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);

            request.AddExtension(fetch);

if (fetch != null)
         {
             email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
             name = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName);
             company = fetch.GetAttributeValue(WellKnownAttributes.Company.CompanyName);
         } 


文章来源: OpenID: Trying to Get Email Address from Google OP