以下是我在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
MSGBOX(电子邮件)
End Sub
但是行
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>
&GT; &LT; dotNetOpenAuth&GT;
<openid>
<relyingParty>
<add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" />
</behaviors>
</relyingParty>
</openid>
&LT; / dotNetOpenAuth&GT;
即便如此,我似乎也获得了空值。我正在从Google获得身份验证。
有人可以帮我吗?
答案 0 :(得分:0)
最后,我找到了解决方案。在这里发布,以便其他面临同样问题的人可以得到帮助。
&#39;全球声明 - 如果您在本地申报,出于某种原因,它们似乎无法正常工作
Dim relyingParty As New OpenIdRelyingParty
Dim authResponse As IAuthenticationResponse
声明后,在您的网络表单上创建一个按钮,允许用户登录Google。还有一个标签,显示已登录用户的电子邮件ID。
&#39;在页面加载上放置以下代码。
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
你已经完成了。