0

以下はvb.netで書かれた私のコードです。

Protected Sub OpenIdButton3_LoggedIn(ByVal sender As Object、ByVal e As DotNetOpenAuth.OpenId.RelyingParty.OpenIdEventArgs)Handles OpenIdButton3.LoggedIn

OpenIdButton3.Visible = False

薄暗いプロファイルAsClaimsResponse= e.Response.GetExtension(Of ClaimsResponse)()

Dim email As String = profile.Email

MsgBox(メール)

サブ終了

しかし、ライン

Dim email As String = profile.Email

次のエラーが発生しています。

例外の詳細:System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。

これに関連するドキュメントを読み、webconfigにAXFetchAsSregTransformを実装しました。以下は同じことを示すブロックです。

<sectionGroup name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection, DotNetOpenAuth.Core">
  <section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true" />
  <section name="openid" type="DotNetOpenAuth.Configuration.OpenIdElement, DotNetOpenAuth.OpenId" requirePermission="false" allowLocation="true" />
  <section name="oauth" type="DotNetOpenAuth.Configuration.OAuthElement, DotNetOpenAuth.OAuth" requirePermission="false" allowLocation="true" />
  <section name="messaging" type="DotNetOpenAuth.Configuration.MessagingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" />
  <section name="reporting" type="DotNetOpenAuth.Configuration.ReportingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" />
</sectionGroup>

> <dotNetOpenAuth>

<openid>

  <relyingParty>

      <add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" />

    </behaviors>

  </relyingParty>

</openid>

</ dotNetOpenAuth>

それでも、null値を取得しているようです。Googleから認証を受けています。

誰かがこれについて私を助けることができますか?

4

1 に答える 1

0

最後に、私は解決策を見つけました。同じ問題に直面している他の人を助けることができるように、ここに投稿してください。

'グローバル宣言-何らかの理由でローカルで宣言すると、機能しないようです

新しいOpenIdRelyingPartyとしての薄暗いrelyingParty

Dim authResponse As IAuthenticationResponse

これを宣言した後、ユーザーがGoogleにログオンできるようにするボタンをWebフォームに作成します。また、ログインしたユーザーの電子メールIDを表示するラベル。

'次のコードをページの読み込みに配置します。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim googleAppsDiscovery As New HostMetaDiscoveryService() With {.UseGoogleHostedHostMeta = True}
    relyingParty.DiscoveryServices.Insert(0, googleAppsDiscovery)

    authResponse = relyingParty.GetResponse()

    If authResponse IsNot Nothing Then
        If authResponse.Status = AuthenticationStatus.Authenticated Then
            Dim fetch As FetchResponse = authResponse.GetExtension(Of FetchResponse)()
            If fetch IsNot Nothing Then
                ' Save user details in session variables
                'Dim temp As [String]() = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email).ToString().Split("@"c)
                Dim temp As String = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email).ToString()
                If temp IsNot Nothing Then
                    Dim userName As String = temp
                    Label1.Text = "You are logged in as : " & userName
                End If
            End If
        End If
    End If
End Sub

ボタンのクリックイベントに次のコードを入れます

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim currentPageURL As String = curPageURL.ToString

    Dim urlRealm As New Uri(currentPageURL)
    Dim urlReturn As New Uri(currentPageURL)

    Dim rm As Realm = New Realm(urlRealm)
    Dim gIdentifier As String = "https://www.google.com/accounts/o8/id"

    Dim request As IAuthenticationRequest = relyingParty.CreateRequest(gIdentifier, urlRealm, urlReturn)

    Dim fetch As New FetchRequest

    fetch.Attributes.Add(New AttributeRequest(WellKnownAttributes.Contact.Email, True))
    request.AddExtension(fetch)

    request.RedirectToProvider()
End Sub

次の2つの関数を追加して、現在のURLを取得します

Function curPageURL() As String
    Dim s, protocol, port As String

    If Request.ServerVariables("HTTPS") = "on" Then
        s = "s"
    Else
        s = ""
    End If

    protocol = strLeft(LCase(Request.ServerVariables("SERVER_PROTOCOL")), "/") & s

    If Request.ServerVariables("SERVER_PORT") = "80" Then
        port = ""
    Else
        port = ":" & Request.ServerVariables("SERVER_PORT")
    End If

    curPageURL = protocol & "://" & Request.ServerVariables("SERVER_NAME") & _
          port & Request.ServerVariables("SCRIPT_NAME")
End Function

Function strLeft(ByVal str1 As String, ByVal str2 As String) As String
    strLeft = Left(str1, InStr(str1, str2) - 1)
End Function

そして、これで完了です。

于 2013-02-15T11:02:53.973 に答える