我一直在過去兩天在網上搜索DotNetOpenAuth中的OAuthConsumer示例的MVC實現,但我仍然沒有找到任何解決方案。 我也嘗試將OAuthConsumer實現從WebForms轉換爲MVC,但仍然無法正確實現它。 任何人都可以請參考一些地方找到一個轉換器樣本。MVC在DotNetOpenAuth中專門爲Google地址簿實現OAuthConsumer
2
A
回答
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中工作。
如果這不起作用,請在您的問題中添加更多內容,以解釋您遇到的問題。
相關問題
- 1. iphone中的地址簿實現
- 2. DotNetOpenAuth和Google OpenID實現
- 3. JBoss - 實現專用閥門
- 4. 可可:如何實現地址簿
- 5. 如何實現大型地址簿?
- 6. iPhone地址簿 - 實際地址
- 7. 使用ASP.net MVC 3.0實現DotNetOpenAuth
- 8. iphone地址簿
- 9. Iphone,地址簿中的地址信息
- 10. DotNetOpenAuth - 實現IDirectWebRequestHandler類
- 11. 在iPhone上爲ABPeoplePickerNavigationController定製地址簿
- 12. Quora是如何實現地址簿導入的?
- 13. 任何人都知道一個好的地址簿實現?
- 14. 在MVC中實現Google Maps地理編碼器
- 15. php中的地址簿
- 16. 地址簿數據
- 17. 地址簿程序
- 18. 地址簿程序
- 19. iOS 8將地址簿添加到地址簿
- 20. 實現OpenID選擇器+ dotnetopenauth
- 21. 在iOS中關閉地址簿
- 22. 使用jQuery和/或Google Maps API創建專門的地圖
- 23. 地址簿常量評價爲零
- 24. 使用地址簿作爲數據源
- 25. 在iPhone上實現地址簿自動完成的最佳方式?
- 26. 專門爲枚舉模板
- 27. 實現直接地址表
- 28. 如何設置DotNetOpenAuth的返回地址
- 29. iPhone地址簿搜索
- 30. outlook 2010虛擬地址簿
Thx安德魯爲您的迴應,經過2天的鬥爭我解決了問題如下(在我的問題的答案),但我認爲它需要一些更多的改進。你可以檢查並確定需要改進的地方嗎? – Abdul