2011-08-23 4 views
2

지난 2 일 동안 MVC Implementation of OAuthConsumer Sample for DotNetOpenAuth에서 그물을 검색했지만 여전히 해결책을 찾지 못했습니다. OAuthConsumer 구현을 WebForms에서 MVC로 변환하려고했지만 여전히 올바르게 구현할 수 없었습니다. 누구든지 변환기 샘플을 찾을 수있는 장소를 참조하여 도움을받을 수 있습니다.MVC DotNetOpenAuth의 OAuthConsumer 구현 특히 Google 주소록

답변

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 소비자는 프레젠테이션 프레임 워크와 아무런 관련이 없으므로 웹 양식과 MVC간에 차이가 없어야합니다. 소비자 관련 코드를 웹 양식 샘플에서 직접 추출하여 MVC에서 사용할 수 있어야합니다.

그래도 문제가 해결되지 않으면보고있는 문제에 대해 설명하는 질문을 추가하십시오.

+0

답장을 보내 주신 2 주 동안의 고민 끝에 다음과 같이 문제가 해결되었지만 개선이 필요하다고 생각됩니다. 개선이 필요한 장소를 확인하고 식별 할 수 있습니까? – Abdul