1

我正在尝试使用 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
4

2 回答 2

3

您是否在 SourceForge 上找到了 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
于 2013-03-27T15:48:27.697 回答
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
于 2013-03-26T18:57:45.443 回答