2013-03-26 120 views
1

我想通過OpenID使用DotNetOpenAuth從Google獲取用戶的電子郵件地址。使用Google OpenID獲取用戶電子郵件地址

到目前爲止,我的代碼已經正確地重定向到Google的當前用戶,並要求我的應用程序允許其閱讀電子郵件地址。然而,在被轉回到我的頁面時,它會直接跳回Google。我明白了爲什麼會發生這種情況(因爲頁面不會進入回髮狀態),但是您如何區分請求和響應數據,以便我可以正確讀取頁面中的電子郵件地址?

是否有推薦/行業標準的方法呢?

我只是剛剛開始使用OpenID和DotNetOpenAuth,但有很強的ASP.NET技巧,所以請保持你的答案明確(!)

感謝

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    ltl.Text = "Welcome " & User.Identity.Name 

    If Not Page.IsPostBack Then 
     Dim openid As New OpenIdRelyingParty 
     Dim req As IAuthenticationRequest = openid.CreateRequest(User.Identity.Name) 
     Dim fetch As New FetchRequest 
     fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email) 
     fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName) 
     req.AddExtension(fetch) 
     req.RedirectToProvider() 
    Else 
     Dim openid As New OpenIdRelyingParty 
     Dim resp As IAuthenticationResponse = openid.GetResponse() 
     If resp IsNot Nothing Then 
      Dim fetch As FetchResponse = resp.GetExtension(Of FetchResponse)() 
      If fetch IsNot Nothing Then 
       Trace.Warn(fetch.GetAttributeValue(WellKnownAttributes.Contact.Email)) 
      Else 
       Trace.Warn("fetch was Nothing") 
      End If 
     Else 
      Trace.Warn("resp was Nothing") 
     End If 
    End If 
End Sub 

回答

3

您是否發現DotNetOpenAuth示例available on SourceForge

這裏是推薦模式:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    ltl.Text = "Welcome " & User.Identity.Name 

    Dim openid As New OpenIdRelyingParty 
    Dim resp As IAuthenticationResponse = openid.GetResponse() 
    If resp Is Nothing Then 
     Dim req As IAuthenticationRequest = openid.CreateRequest(User.Identity.Name) 
     Dim fetch As New FetchRequest 
     fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email) 
     fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName) 
     req.AddExtension(fetch) 
     req.RedirectToProvider() 
    Else 
     Dim fetch As FetchResponse = resp.GetExtension(Of FetchResponse)() 
     If fetch IsNot Nothing Then 
      Trace.Warn(fetch.GetAttributeValue(WellKnownAttributes.Contact.Email)) 
     Else 
      Trace.Warn("fetch was Nothing") 
     End If 
    End If 
End Sub 
+0

你是對的。我很失望,我不認爲只是檢查是否給出了答覆。 – gowansg 2013-03-27 18:51:22

+0

感謝您的輸入安德魯。我不知道代碼示例,如果它們與上面的代碼示例一樣簡潔,它們將非常有用。我昨天確實自己解決了這個問題,但看到你的例子我仍然有改進的空間。另一方面,在昨天在網上搜索代碼樣本時,我在各個網站上閱讀了很多你的帖子。感謝您在這方面的不懈努力和貢獻。它使像我這樣的開發人員的生活變得更加簡單:-) – EvilDr 2013-03-28 15:33:44

1

而不是檢查IsPostBack你的可以添加一個查詢字符串參數並檢查它的存在。您可以在用戶請求登錄或提供程序返回時執行此操作。

以下代碼片段檢查是否存在查詢字符串參數,該參數表示提供程序已重定向回您的頁面。

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
    ltl.Text = "Welcome " & User.Identity.Name 

    If Not Request.QueryString(your_parameter) Is Nothing Then 
     'Read Response 
     ... 
    Else 
     'Send Request 
     Dim openid As New OpenIdRelyingParty 
     Dim req As IAuthenticationRequest = openid.CreateRequest(User.Identity.Name) 

     'Specify the url the provider should redirect to 
     'this would be whatever url brings you to this page plus query string 
     req.AddCallbackArguments("returnUrl", your_url); 
     ... 
    End If 
End Sub 
+0

喜是該感謝。這是我的第一個想法,但我很想知道是否有DotNetOpenAuth的這種行業標準方法...? – EvilDr 2013-03-27 08:34:47

+1

不需要人工回調參數 – 2013-03-27 15:45:36

相關問題