diff Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 100:cc91817a4206

Merge
author jorge.rowies
date Sat, 04 Jun 2011 22:46:06 -0300
parents 1eb5a0e531bf
children 1ee5711256db
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Sat Jun 04 22:33:05 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs	Sat Jun 04 22:46:06 2011 -0300
@@ -1,7 +1,12 @@
-using System.Web.Mvc;
+using System;
+using System.Web.Mvc;
 using System.Web.Routing;
 using System.Web.Security;
+using AltNetHispano.Agendas.Domain;
+using AltNetHispano.Agendas.Factories;
+using AltNetHispano.Agendas.Twitter;
 using AltNetHispano.Agendas.Web.Models;
+using AltNetHispano.Agendas.Web.Services;
 
 namespace AltNetHispano.Agendas.Web.Controllers
 {
@@ -33,29 +38,89 @@
         {
             if (ModelState.IsValid)
             {
-                if (MembershipService.ValidateUser(model.UserName, model.Password))
-                {
-                    FormsService.SignIn(model.UserName, model.RememberMe);
-                    if (Url.IsLocalUrl(returnUrl))
-                    {
-                        return Redirect(returnUrl);
-                    }
-                    else
-                    {
-                        return RedirectToAction("Index", "Home");
-                    }
-                }
-                else
-                {
-                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
-                }
+				if (MembershipService.ValidateUser(model.UserName, model.Password))
+				{
+					var personaService = AgendaFactory.GetPersonaService();
+					if (personaService.Validate(IdentityProviderEnum.BuiltIn, model.UserName))
+					{
+						FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.BuiltIn] + model.UserName, model.RememberMe);
+						if (Url.IsLocalUrl(returnUrl))
+							return Redirect(returnUrl);
+						return RedirectToAction("Index", "Home");
+					}
+				}
+            	ModelState.AddModelError("", "The user name or password provided is incorrect.");
             }
 
             // If we got this far, something failed, redisplay form
             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.
+				var action = Url.Action("TwitterLogOn");
+				var url = Request.Url.Scheme + "://" + Request.Url.Host +
+				          (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;
+
+				oAuth.CallBackUrl = url;
+				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");
+					var nombre = GetXmlContent(xml, "name");
+
+					var personaService = AgendaFactory.GetPersonaService();
+					if (personaService.Validate(IdentityProviderEnum.Twitter, username, nombre))
+					{
+						FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false);
+						return RedirectToAction("Index", "Home");
+					}
+					ModelState.AddModelError("", "The user name or password provided is incorrect.");
+
+
+					//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 static 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
         // **************************************
 
@@ -86,13 +151,14 @@
 
                 if (createStatus == MembershipCreateStatus.Success)
                 {
-                    FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
-                    return RedirectToAction("Index", "Home");
+					var personaService = AgendaFactory.GetPersonaService();
+					if (personaService.Validate(IdentityProviderEnum.BuiltIn, model.UserName, model.Nombre))
+					{
+						FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.BuiltIn] + model.UserName, false);
+						return RedirectToAction("Index", "Home");
+					}
                 }
-                else
-                {
-                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
-                }
+                ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
             }
 
             // If we got this far, something failed, redisplay form