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

使用Google OpenID获取用户的电子邮件地址,openid,dotnetopenauth,asp.net-4.5,google-openid,Openid,Dotnetopenauth,Asp.net 4.5,Google Openid,我正在尝试使用DotNetOpenAuth通过OpenID从谷歌检索用户的电子邮件地址 到目前为止,我的代码正确地重定向到当前用户的Google,并请求允许我的应用程序读取电子邮件地址。然而,当被转移回我的页面时,它会直接返回到谷歌。我明白为什么会发生这种情况(因为页面从未进入回发状态),但您如何区分请求和响应数据,以便我能够正确读取页面中的电子邮件地址 有推荐的/行业标准的方法吗? 我刚开始学习OpenID和DotNetOpenAuth,但我有很强的ASP.NET技能,所以请保持你的答案清晰(

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

到目前为止,我的代码正确地重定向到当前用户的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

您可以添加查询字符串参数并检查其是否存在,而不是检查
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

您可以添加查询字符串参数并检查其是否存在,而不是检查
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

您找到DotNetOpenAuth示例了吗

以下是推荐的模式:

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

您找到DotNetOpenAuth示例了吗

以下是推荐的模式:

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

嗨,是的,谢谢你。这是我的第一个想法,但我很想知道是否有一个行业标准的方法来使用DotNetOpenAuth…?不需要人工回调参数Hi是的,谢谢。这是我的第一个想法,但我很想知道是否有一个行业标准的方法来使用DotNetOpenAuth…?不需要人工回调参数。你是对的。我很失望,我没想到只是检查一下是否有人回复。谢谢你的意见。我不知道这些代码示例,如果它们像上面的代码示例一样简洁,那么它们将非常有用。我昨天确实自己解决了这个问题,但看到你的例子后,我还有改进的余地。另一方面,当我昨天在网上搜寻代码样本时,我读了很多你在不同网站上的帖子。感谢您在这方面的持续努力和贡献。这让像我这样的开发人员的生活变得更加轻松:-)你说得对。我很失望,我没想到只是检查一下是否有人回复。谢谢你的意见。我不知道这些代码示例,如果它们像上面的代码示例一样简洁,那么它们将非常有用。我昨天确实自己解决了这个问题,但看到你的例子后,我还有改进的余地。另一方面,当我昨天在网上搜寻代码样本时,我读了很多你在不同网站上的帖子。感谢您在这方面的持续努力和贡献。它让像我这样的开发人员的生活变得更加轻松:-)