使用Google OpenID获取用户电子邮件地址

时间:2013-03-26 17:14:21

标签: openid dotnetopenauth asp.net-4.5 google-openid

我正在尝试使用DotNetOpenAuth通过OpenID从Google检索用户的电子邮件地址。

到目前为止,我的代码已正确地重定向到当前用户的Google,并要求我的应用程序允许其阅读电子邮件地址。然而,当被转移回我的页面时,它会直接反弹回谷歌。我知道为什么会发生这种情况(因为页面永远不会进入回发状态),但是如何区分请求和响应数据,以便我能正确读取页面中的电子邮件地址?

是否有针对此的推荐/行业标准方法?

我刚刚开始使用OpenID和DotNetOpenAuth但具有很强的ASP.NET技能,所以请保持清楚的答案(!)

由于

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    ltl.Text = "Welcome " & User.Identity.Name

    If Not Page.IsPostBack Then
        Dim openid As New OpenIdRelyingParty
        Dim req As IAuthenticationRequest = openid.CreateRequest(User.Identity.Name)
        Dim fetch As New FetchRequest
        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email)
        fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName)
        req.AddExtension(fetch)
        req.RedirectToProvider()
    Else
        Dim openid As New OpenIdRelyingParty
        Dim resp As IAuthenticationResponse = openid.GetResponse()
        If resp IsNot Nothing Then
            Dim fetch As FetchResponse = resp.GetExtension(Of FetchResponse)()
            If fetch IsNot Nothing Then
                Trace.Warn(fetch.GetAttributeValue(WellKnownAttributes.Contact.Email))
            Else
                Trace.Warn("fetch was Nothing")
            End If
        Else
            Trace.Warn("resp was Nothing")
        End If
    End If
End Sub

2 个答案:

答案 0 :(得分:3)

您是否找到了DotNetOpenAuth样本available on SourceForge

以下是推荐的模式:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    ltl.Text = "Welcome " & User.Identity.Name

    Dim openid As New OpenIdRelyingParty
    Dim resp As IAuthenticationResponse = openid.GetResponse()
    If resp Is Nothing Then
        Dim req As IAuthenticationRequest = openid.CreateRequest(User.Identity.Name)
        Dim fetch As New FetchRequest
        fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email)
        fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName)
        req.AddExtension(fetch)
        req.RedirectToProvider()
    Else
        Dim fetch As FetchResponse = resp.GetExtension(Of FetchResponse)()
        If fetch IsNot Nothing Then
            Trace.Warn(fetch.GetAttributeValue(WellKnownAttributes.Contact.Email))
        Else
            Trace.Warn("fetch was Nothing")
        End If
    End If
End Sub

答案 1 :(得分:1)

您可以添加查询字符串参数并检查其是否存在,而不是检查IsPostBack。您可以在用户请求登录或提供商返回时执行此操作。

下面的代码片段会检查是否存在查询字符串参数,该参数表明提供商已将其重定向回您的网页。

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    ltl.Text = "Welcome " & User.Identity.Name

    If Not Request.QueryString(your_parameter) Is Nothing Then
        'Read Response
        ...
    Else
        'Send Request
        Dim openid As New OpenIdRelyingParty
        Dim req As IAuthenticationRequest = openid.CreateRequest(User.Identity.Name)

        'Specify the url the provider should redirect to
        'this would be whatever url brings you to this page plus query string 
        req.AddCallbackArguments("returnUrl", your_url);
        ...
    End If
End Sub