我一直在過去兩天在網上搜索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. 爲dotnetopenauth實現IConsumerTokenManager
- 5. 可可:如何實現地址簿
- 6. 如何實現大型地址簿?
- 7. iPhone地址簿 - 實際地址
- 8. 在DotNetOpenAuth中實現IConsumerTokenManager
- 9. 的iOS地址簿記錄現有的地址簿數據庫
- 10. 使用ASP.net MVC 3.0實現DotNetOpenAuth
- 11. DotNetOpenAuth - 實現IDirectWebRequestHandler類
- 12. iOS:地址簿
- 13. 地址簿peoplePickerNavigationController
- 14. iphone地址簿
- 15. Iphone,地址簿中的地址信息
- 16. 在iPhone上爲ABPeoplePickerNavigationController定製地址簿
- 17. 現場`referent`專門由GC
- 18. 任何人都知道一個好的地址簿實現?
- 19. Quora是如何實現地址簿導入的?
- 20. 在MVC中實現Google Maps地理編碼器
- 21. php中的地址簿
- 22. Popover中的地址簿
- 23. 專門實施內部類
- 24. Netsuite:有沒有辦法擁有專門的裝運地址和帳單地址?
- 25. 地址簿程序
- 26. IOS 6地址簿
- 27. 地址簿數據
- 28. 地址簿C#GUI
- 29. 地址簿教程
- 30. 地址簿程序
Thx安德魯爲您的迴應,經過2天的鬥爭我解決了問題如下(在我的問題的答案),但我認爲它需要一些更多的改進。你可以檢查並確定需要改進的地方嗎? – Abdul