2013-02-14 69 views
0

以下是我用vb.net編寫的代碼。OpenIdButton的擴展返回null

保護小組OpenIdButton3_LoggedIn(BYVAL發件人爲對象,BYVALË作爲DotNetOpenAuth.OpenId.RelyingParty.OpenIdEventArgs)把手OpenIdButton3.LoggedIn

OpenIdButton3.Visible =假

昏暗簡介作爲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獲得身份驗證。

任何人都可以幫助我嗎?

回答

0

最後,我找到了解決方案。在這裏發佈,以便其他面臨同樣問題的人可以得到幫助。

「全球宣言 - 如果你在本地聲明它們,出於某種原因,他們不這樣做似乎工作

昏暗relyingParty作爲新OpenIdRelyingParty

昏暗authResponse作爲IAuthenticationResponse

聲明後,在您的web表單上創建一個按鈕,這將允許用戶登錄到Google。標籤將顯示已登錄的用戶的電子郵件ID。

'將下面的代碼放在頁面加載中。在按鈕的Click事件

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 

和你做。