0

以下是我用 vb.net 编写的代码。

Protected Sub OpenIdButton3_LoggedIn(ByVal sender As Object, ByVal e As DotNetOpenAuth.OpenId.RelyingParty.OpenIdEventArgs) 处理 OpenIdButton3.LoggedIn

OpenIdButton3.Visible = False

暗淡的个人资料 As ClaimsResponse = e.Response.GetExtension(Of ClaimsResponse)()

暗淡电子邮件作为字符串 = profile.Email

MsgBox(电子邮件)

结束子

但线

暗淡电子邮件作为字符串 = 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>

即使那样,我似乎也得到了空值。我正在从 Google 获得身份验证。

谁可以帮我这个事?

4

1 回答 1

0

最后,我找到了解决方案。在这里发布它,以便其他面临相同问题的人可以得到帮助。

'全球声明 - 如果您在本地声明它们,出于某种原因,它们似乎不起作用

将依赖方调暗为新的 OpenIdRelyingParty

将 authResponse 调暗为 IAuthenticationResponse

声明后在您的网络表单上创建一个按钮,允许用户登录 Google。还有一个标签,将显示已登录用户的电子邮件 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

添加以下两个函数以获取您当前的 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 回答