【问题标题】:OpenIdButton's extension returns nullOpenIdButton 的扩展返回 null
【发布时间】:2013-02-15 19:02:53
【问题描述】:

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

受保护的子 OpenIdButton3_LoggedIn(ByVal sender As Object, ByVal e As DotNetOpenAuth.OpenId.RelyingParty.OpenIdEventArgs) 处理 OpenIdButton3.LoggedIn

OpenIdButton3.Visible = False

Dim profile As ClaimsResponse = e.Response.GetExtension(Of ClaimsResponse)()

Dim email As String = profile.Email

消息框(电子邮件)

结束子

但线

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>

>

<openid>

  <relyingParty>

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

    </behaviors>

  </relyingParty>

</openid>

即便如此,我似乎也得到了空值。我正在从 Google 获得身份验证。

谁能帮我解决这个问题?

【问题讨论】:

    标签: openid dotnetopenauth


    【解决方案1】:

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

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

    将依赖方调暗为新的 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
    

    添加以下两个函数来获取您当前的网址

    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
    

    你就完成了。

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2017-02-24
      • 2015-10-04
      • 2023-03-05
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      相关资源
      最近更新 更多