2011-08-23 58 views
2

我一直在過去兩天在網上搜索DotNetOpenAuth中的OAuthConsumer示例的MVC實現,但我仍然沒有找到任何解決方案。 我也嘗試將OAuthConsumer實現從WebForms轉換爲MVC,但仍然無法正確實現它。 任何人都可以請參考一些地方找到一個轉換器樣本。MVC在DotNetOpenAuth中專門爲Google地址簿實現OAuthConsumer

回答

1

經過2天的鬥爭,我解決了這個問題如下,但我認爲它需要更多的改進。

private string AccessToken 
{ 
    get { return (string)Session["GoogleAccessToken"]; } 
    set { Session["GoogleAccessToken"] = value; } 
} 

private InMemoryTokenManager TokenManager 
{ 
    get 
    { 
     var tokenManager = (InMemoryTokenManager)HttpContext.Application["GoogleTokenManager"]; 
     if (tokenManager == null) 
     { 
      string consumerKey = ConfigurationManager.AppSettings["GoogleOAuthConsumerKey"]; 
      string consumerSecret = ConfigurationManager.AppSettings["GoogleOAuthConsumerValue"]; 
      if (!string.IsNullOrEmpty(consumerKey)) 
      { 
       tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret); 
       HttpContext.Application["GoogleTokenManager"] = tokenManager; 
      } 
     } 

     return tokenManager; 
    } 
} 


public ActionResult GoogleSync() 
{ 
    var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager); 

    // Is Google calling back with authorization? 
    var accessTokenResponse = google.ProcessUserAuthorization(); 
    if (accessTokenResponse != null) 
    { 
     this.AccessToken = accessTokenResponse.AccessToken; 
     XDocument contactsDocument = GoogleConsumer.GetContacts(google, this.AccessToken, 5, 1); 
     var contactList = new List<GMailContact>(); 
     foreach (var entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom"))) 
     { 
      GMailContact newContact = new GMailContact { Name = string.Empty, Email = string.Empty }; 
      var titleElement = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")); 
      if (titleElement != null) 
       newContact.Name = titleElement.Value; 
      var emailElement = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")); 
      if (emailElement != null && emailElement.Attribute("address") != null) 
      { 
       newContact.Email = emailElement.Attribute("address").Value; 
      } 

      contactList.Add(newContact); 
     } 

     ////var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")) 
     ////    select new { Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value, 
     ////       Email = (XName.Get("email", "http://schemas.google.com/g/2005") == null ? "" : entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value) }; 

     return View(contactList); 
    } 
    else if (this.AccessToken == null) 
    { 
     // If we don't yet have access, immediately request it. 
     GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts); 

     return this.Content(""); 
    } 
    else 
    { 

     return this.Content("synchronization failed."); 
    } 

} 
0

我知道沒有任何OAuth消費者的MVC示例。但是,由於OAuth消費者確實與表示框架無關,所以在Web表單和MVC之間不應該有任何不同。您應該能夠直接從Web表單示例中提取與消費者相關的代碼,並使其在MVC中工作。

如果這不起作用,請在您的問題中添加更多內容,以解釋您遇到的問題。

+0

Thx安德魯爲您的迴應,經過2天的鬥爭我解決了問題如下(在我的問題的答案),但我認爲它需要一些更多的改進。你可以檢查並確定需要改進的地方嗎? – Abdul