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 に答える