diff Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 90:d1688622fa88

Autenticando con twitter (falta emprolijar el código, pero autentica!)
author Nelo@Kenia.neluz.int
date Fri, 03 Jun 2011 21:35:59 -0300
parents 475be11edf56
children 7027cda13de3
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Tue May 31 16:31:24 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Fri Jun 03 21:35:59 2011 -0300
@@ -1,6 +1,8 @@
-using System.Web.Mvc;
+using System;
+using System.Web.Mvc;
 using System.Web.Routing;
 using System.Web.Security;
+using AltNetHispano.Agendas.Twitter;
 using AltNetHispano.Agendas.Web.Models;
 
 namespace AltNetHispano.Agendas.Web.Controllers
@@ -55,7 +57,61 @@
             return View(model);
         }
 
-        // **************************************
+		public ActionResult TwitterLogOn()
+		{
+			var oAuth = new oAuthTwitter();
+
+			if (Request["oauth_token"] == null)
+			{
+				//Redirect the user to Twitter for authorization.
+				//Using oauth_callback for local testing.
+				oAuth.CallBackUrl = "http://localhost:1424/Account/TwitterLogOn";
+				Response.Redirect(oAuth.AuthorizationLinkGet());
+			}
+			else
+			{
+				//Get the access token and secret.
+				oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
+				if (oAuth.TokenSecret.Length > 0)
+				{
+					//We now have the credentials, so make a call to the Twitter API.
+					const string url = "http://twitter.com/account/verify_credentials.xml";
+					string xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty);
+
+					var username = GetXmlContent(xml, "screen_name");
+					//TODO: Validar que este usuario de twitter corresponde a un usuario del sitio
+
+					FormsService.SignIn(username, false);
+					return RedirectToAction("Index", "Home");
+
+					//POST Test
+					//url = "http://twitter.com/statuses/update.xml";
+					//xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, "status=" + oAuth.UrlEncode("Hello @swhitley - Testing the .NET oAuth API"));
+					//apiResponse.InnerHtml = Server.HtmlEncode(xml);
+				}
+			}
+
+			return RedirectToAction("LogOn");
+		}
+
+    	private string GetXmlContent(string xml, string tagName)
+    	{
+    		var tagStart = "<" + tagName + ">";
+    		var tagEnd = "</" + tagName + ">";
+
+    		var start = xml.IndexOf(tagStart)+tagStart.Length;
+			if (start>tagStart.Length)
+			{
+				var end = xml.IndexOf(tagEnd, start);
+				if (end>0)
+				{
+					return xml.Substring(start, end - start);
+				}
+			}
+    		return string.Empty;
+    	}
+
+    	// **************************************
         // URL: /Account/LogOff
         // **************************************