comparison 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
comparison
equal deleted inserted replaced
89:24e9488ac152 90:d1688622fa88
1 using System.Web.Mvc; 1 using System;
2 using System.Web.Mvc;
2 using System.Web.Routing; 3 using System.Web.Routing;
3 using System.Web.Security; 4 using System.Web.Security;
5 using AltNetHispano.Agendas.Twitter;
4 using AltNetHispano.Agendas.Web.Models; 6 using AltNetHispano.Agendas.Web.Models;
5 7
6 namespace AltNetHispano.Agendas.Web.Controllers 8 namespace AltNetHispano.Agendas.Web.Controllers
7 { 9 {
8 public class AccountController : Controller 10 public class AccountController : Controller
53 55
54 // If we got this far, something failed, redisplay form 56 // If we got this far, something failed, redisplay form
55 return View(model); 57 return View(model);
56 } 58 }
57 59
58 // ************************************** 60 public ActionResult TwitterLogOn()
61 {
62 var oAuth = new oAuthTwitter();
63
64 if (Request["oauth_token"] == null)
65 {
66 //Redirect the user to Twitter for authorization.
67 //Using oauth_callback for local testing.
68 oAuth.CallBackUrl = "http://localhost:1424/Account/TwitterLogOn";
69 Response.Redirect(oAuth.AuthorizationLinkGet());
70 }
71 else
72 {
73 //Get the access token and secret.
74 oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
75 if (oAuth.TokenSecret.Length > 0)
76 {
77 //We now have the credentials, so make a call to the Twitter API.
78 const string url = "http://twitter.com/account/verify_credentials.xml";
79 string xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, url, String.Empty);
80
81 var username = GetXmlContent(xml, "screen_name");
82 //TODO: Validar que este usuario de twitter corresponde a un usuario del sitio
83
84 FormsService.SignIn(username, false);
85 return RedirectToAction("Index", "Home");
86
87 //POST Test
88 //url = "http://twitter.com/statuses/update.xml";
89 //xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, "status=" + oAuth.UrlEncode("Hello @swhitley - Testing the .NET oAuth API"));
90 //apiResponse.InnerHtml = Server.HtmlEncode(xml);
91 }
92 }
93
94 return RedirectToAction("LogOn");
95 }
96
97 private string GetXmlContent(string xml, string tagName)
98 {
99 var tagStart = "<" + tagName + ">";
100 var tagEnd = "</" + tagName + ">";
101
102 var start = xml.IndexOf(tagStart)+tagStart.Length;
103 if (start>tagStart.Length)
104 {
105 var end = xml.IndexOf(tagEnd, start);
106 if (end>0)
107 {
108 return xml.Substring(start, end - start);
109 }
110 }
111 return string.Empty;
112 }
113
114 // **************************************
59 // URL: /Account/LogOff 115 // URL: /Account/LogOff
60 // ************************************** 116 // **************************************
61 117
62 public ActionResult LogOff() 118 public ActionResult LogOff()
63 { 119 {