如何使用DotNetOpenAuth检索谷歌用户的emailId

时间:2010-12-27 22:17:45

标签: dotnetopenauth

我正在尝试使用 DotNetOpenAuthto 经过身份验证的用户,我可以这样做。但我无法找到一种方法来获取谷歌提供的其他参数,如电子邮件,名字,姓氏等。

我使用的代码段是:

<%@ Register Assembly="DotNetOpenAuth" Namespace="DotNetOpenAuth.OpenId.RelyingParty" TagPrefix="rp" %>

...............................

<rp:OpenIdButton runat="server" ID="OpenIdTextButton1"
    OnLoggedIn="OpenIdTextBox1_LoggedIn"
    Text="aaaa"
    Identifier="https://www.google.com/accounts/o8/id" 
    />

1 个答案:

答案 0 :(得分:4)

我的博文How to pretty much guarantee that you might get an email address with OpenID中有几个提示。

简而言之,您需要activate the AXFetchAsSregTransform behavior

<configuration>
   <configSections>
      <section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true"/>
   </configSections>
   <dotNetOpenAuth>
      <openid>
         <relyingParty>
            <behaviors>
               <!-- The following OPTIONAL behavior allows RPs to use SREG only, but be compatible
                    with OPs that use Attribute Exchange (in various formats). -->
               <add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" />
            </behaviors>
         </relyingParty>
      </openid>
   </dotNetOpenAuth>
</configuration>

然后您需要告知提供商您需要用户的电子邮件地址。理想情况下,这将是这样做的:

<rp:OpenIdButton runat="server"
    Text="Log in with Google"
    Identifier="https://www.google.com/accounts/o8/id">
    <Extensions>
        <sreg:ClaimsRequest Email="Require" />
    </Extensions>
</rp:OpenIdButton>

然而,DotNetOpenAuth(v3.4.7修复此问题)中存在一个错误,导致<Extensions>标记无法在OpenIdButton上运行。因此,您必须在代码隐藏中添加属性请求。所以你的标签看起来像是:

<rp:OpenIdButton runat="server"
    Text="Log in with Google"
    OnLoggingIn="OpenId_LoggingIn" 
    Identifier="https://www.google.com/accounts/o8/id" />

你的代码隐藏有这种方法:

protected void OpenId_LoggingIn(object sender, OpenIdEventArgs e) {
    e.Request.AddExtension(new ClaimsRequest() { Email = DemandLevel.Require });
}