【问题标题】:OpenID: Trying to Get Email Address from Google OPOpenID:尝试从 Google OP 获取电子邮件地址
【发布时间】:2009-08-19 16:47:27
【问题描述】:

我正在使用 dotnetopenauth 3.2 来实现 Openid,但无法弄清楚如何让 Google 在声明响应中传递电子邮件地址。我知道谷歌不支持简单的注册,但我无法确定他们支持什么。

对这个问题的警告是,我刚开始学习 OpenID,我知道我对规范没有扎实的掌握,我认为这会导致我的困惑。

任何帮助将不胜感激!

【问题讨论】:

    标签: c# openid dotnetopenauth


    【解决方案1】:

    好的,想通了。我在Goolge's Federated Log API group 上发布了一个问题,并被告知使用Attribute exchange

    下面是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;
            }
            ...
        }
    }
    

    【讨论】:

    • 这很棒。您是否也知道如何获取提供者名称(除了分析响应)?
    • 您的意思是声称的ID吗? case AuthenticationStatus.Authenticated: { 字符串标识符 = response.ClaimedIdentifier; }
    • 我认为他是在谈论提供商的实际友好名称,即:“Google”“Facebook”,无需解析响应。
    • 借助上述代码,您还可以获得用户的名字和姓氏 - 感谢 zaffiro
    • fetch 对我来说总是空的,知道为什么/
    【解决方案2】:

    当我尝试获取全名时,响应为空,请提供获取全名的解决方案, 这个帖子真的很有帮助 感谢。 我的示例代码是这样的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多