comparison Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 107:1ee5711256db

Utilizando Twitterizer en adapter a Twitter
author Nelo@Kenia.neluz.int
date Tue, 07 Jun 2011 21:03:56 -0300
parents 1eb5a0e531bf
children 0bca45e1e664
comparison
equal deleted inserted replaced
97:2434c2323f3d 107:1ee5711256db
60 { 60 {
61 var oAuth = new OAuthTwitter(); 61 var oAuth = new OAuthTwitter();
62 62
63 if (Request["oauth_token"] == null) 63 if (Request["oauth_token"] == null)
64 { 64 {
65 //Redirect the user to Twitter for authorization.
66 //Using oauth_callback for local testing.
67 var action = Url.Action("TwitterLogOn"); 65 var action = Url.Action("TwitterLogOn");
68 var url = Request.Url.Scheme + "://" + Request.Url.Host + 66 var url = Request.Url.Scheme + "://" + Request.Url.Host +
69 (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action; 67 (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;
70 68
71 oAuth.CallBackUrl = url; 69 Response.Redirect(oAuth.AuthorizationLinkGet(url).ToString());
72 Response.Redirect(oAuth.AuthorizationLinkGet());
73 } 70 }
74 else 71 else
75 { 72 {
76 //Get the access token and secret. 73 var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
77 oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]); 74 if (response.Length > 0)
78 if (oAuth.TokenSecret.Length > 0)
79 { 75 {
80 //We now have the credentials, so make a call to the Twitter API. 76 var username = GetResponseContent(response, "screen_name");
81 const string url = "http://twitter.com/account/verify_credentials.xml"; 77 var nombre = GetResponseContent(response, "name");
82 string xml = oAuth.OAuthWebRequest(OAuthTwitter.Method.GET, url, String.Empty);
83
84 var username = GetXmlContent(xml, "screen_name");
85 var nombre = GetXmlContent(xml, "name");
86 78
87 var personaService = AgendaFactory.GetPersonaService(); 79 var personaService = AgendaFactory.GetPersonaService();
88 if (personaService.Validate(IdentityProviderEnum.Twitter, username, nombre)) 80 if (personaService.Validate(IdentityProviderEnum.Twitter, username, nombre))
89 { 81 {
90 FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false); 82 FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false);
91 return RedirectToAction("Index", "Home"); 83 return RedirectToAction("Index", "Home");
92 } 84 }
93 ModelState.AddModelError("", "The user name or password provided is incorrect."); 85 ModelState.AddModelError("", "The user name or password provided is incorrect.");
94
95
96 //POST Test
97 //url = "http://twitter.com/statuses/update.xml";
98 //xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, "status=" + oAuth.UrlEncode("Hello @swhitley - Testing the .NET oAuth API"));
99 //apiResponse.InnerHtml = Server.HtmlEncode(xml);
100 } 86 }
101 } 87 }
102 88
103 return RedirectToAction("LogOn"); 89 return RedirectToAction("LogOn");
104 } 90 }
105 91
106 private static string GetXmlContent(string xml, string tagName) 92 private static string GetResponseContent(string response, string tagName)
107 { 93 {
108 var tagStart = "<" + tagName + ">"; 94 var tagStart = "\"" + tagName + "\":";
109 var tagEnd = "</" + tagName + ">"; 95 var tagEnd = ",";
110 96
111 var start = xml.IndexOf(tagStart)+tagStart.Length; 97 var start = response.IndexOf(tagStart) + tagStart.Length;
112 if (start>tagStart.Length) 98 if (start > tagStart.Length)
113 { 99 {
114 var end = xml.IndexOf(tagEnd, start); 100 var end = response.IndexOf(tagEnd, start);
115 if (end>0) 101 if (end > 0)
116 { 102 {
117 return xml.Substring(start, end - start); 103 return response.Substring(start + 1, end - start - 2);
118 } 104 }
119 } 105 }
120 return string.Empty; 106 return string.Empty;
121 } 107 }
122 108
123 // ************************************** 109 // **************************************
124 // URL: /Account/LogOff 110 // URL: /Account/LogOff
125 // ************************************** 111 // **************************************
126 112