以下是我在vb.net中编写的代码。
受保护的子OpenIdButton3_LoggedIn(ByVal sender As Object,ByVal e As DotNetOpenAuth.OpenId.RelayingParty.OpenIdEventArgs)处理中的OpenIdButton 3.LoggedIn
OpenIdButton3.Visible=错误
Dim profile As ClaimsRespone=e.Response.GetExtension(索赔响应的)()
将电子邮件Dim为String=配置文件。电子邮件
MsgBox(电子邮件)
结束子
但是线路
将电子邮件Dim为String=配置文件。电子邮件
正在给出以下错误。
异常详细信息: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>
即便如此,我似乎还是得到了空值。我正在从谷歌获得身份验证。
有人能帮我吗?
最后,我找到了解决方案。把它张贴在这里,这样其他面临同样问题的人可以得到帮助。
全球申报-如果你在当地申报,出于某种原因,它们似乎不起的作用
Dim relyingParty作为新的OpenIdRelying聚会
Dim authResponse As IAuthenticationResponse
声明后,在您的网页表单上创建一个按钮,允许用户登录谷歌。还有一个标签,显示登录用户的电子邮件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
你完了。