2009-08-19 52 views
35

我正在使用dotnetopenauth 3.2來實現Openid,但無法弄清楚如何讓Google在索賠響應中傳遞電子郵件地址。我知道Google不支持簡單的註冊,但我無法確定他們的支持。OpenID:試圖從谷歌獲得電子郵件地址OP

買者對這個問題是我剛開始學習的OpenID,我知道我沒有關於這一點我認爲是導致我的困惑規範紮實掌握。

任何幫助,將不勝感激!

回答

52

好吧,算了一下。我發佈了一個關於Goolge's Federated Log API group的問題,並被告知使用Attribute exchange

以下是DotNetOpenAuth的代碼。

請不要在生產中使用此代碼。這僅用於說明目的!

的請求:

using (OpenIdRelyingParty openid = new OpenIdRelyingParty()) 
{ 
    IAuthenticationRequest request = openid.CreateRequest(openidurl); 

    var fetch = new FetchRequest(); 
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); 
    request.AddExtension(fetch); 

    // Send your visitor to their Provider for authentication. 
    request.RedirectToProvider(); 
} 

的迴應:

OpenIdRelyingParty openid = new OpenIdRelyingParty(); 
var response = openid.GetResponse(); 
if (response != null) 
{ 
    switch (response.Status) 
    { 
     case AuthenticationStatus.Authenticated: 
     { 
      var fetch = response.GetExtension<FetchResponse>(); 
      string email = string.Empty(); 
      if (fetch != null) 
      { 
       email = fetch.GetAttributeValue(
        WellKnownAttributes.Contact.Email); 
      } 

      FormsAuthentication.RedirectFromLoginPage(
       response.ClaimedIdentifier, false); 
      break; 
     } 
     ... 
    } 
} 
+0

這是偉大的。你會不會知道如何獲得提供者的名稱(除了分析響應)? – 2010-11-27 06:57:58

+0

你的意思是聲稱的ID? 情況AuthenticationStatus.Authenticated: { 字符串標識符= response.ClaimedIdentifier; } – Rob 2010-12-16 23:51:54

+0

我認爲他在談論提供商的實際友好名稱,即:「Google」「Facebook」無法解析響應。 – Brian 2011-01-02 23:53:15

1

當我試圖讓全名的響應爲空,請提供一個解決方案,以獲得全名, 這帖子真的有幫助 謝謝。 我的示例代碼是這樣的。

var fetch = new FetchRequest(); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName); 
      fetch.Attributes.AddRequired(WellKnownAttributes.Company.CompanyName); 
      //fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); 

      request.AddExtension(fetch); 

if (fetch != null) 
     { 
      email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); 
      name = fetch.GetAttributeValue(WellKnownAttributes.Name.FullName); 
      company = fetch.GetAttributeValue(WellKnownAttributes.Company.CompanyName); 
     } 
相關問題