view Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 242:5cc1681d3789

Combinar
author juanjose.montesdeocaarbos
date Wed, 05 Oct 2011 07:57:21 -0300
parents b921a0ab8504
children 72a96459f910
line wrap: on
line source

using System.Web.Mvc;
using System.Web.Routing;
using AltNetHispano.Agendas.Domain;
using AltNetHispano.Agendas.Factories;
using AltNetHispano.Agendas.Twitter;
using AltNetHispano.Agendas.Web.Services;

namespace AltNetHispano.Agendas.Web.Controllers
{
    public class AccountController : Controller
    {
    	private IFormsAuthenticationService _formsService;

        protected override void Initialize(RequestContext requestContext)
        {
            if (_formsService == null) { _formsService = new FormsAuthenticationService(); }
            
			base.Initialize(requestContext);
        }

        public ActionResult LogOn()
        {
            return View();
        }

		public ActionResult TwitterLogOn(string returnUrl)
		{
			var oAuth = new OAuthTwitter();

			if (Request["oauth_token"] == null)
			{
				var action = Url.Action("TwitterLogOn");
				if (!string.IsNullOrWhiteSpace(returnUrl))
					action += "?ReturnUrl=" + returnUrl;

				var url = Request.Url.Scheme + "://" + Request.Url.Host +
						  (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;

				return Redirect(oAuth.AuthorizationLinkGet(url).ToString());
			}
			
			var response = oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
			if (response.Length > 0)
			{
				var username = OAuthTwitter.GetResponseContent(response, "screen_name");
				var nombre = OAuthTwitter.GetResponseContent(response, "name");
				//TOD: Esto es temporal.
				nombre = nombre.Replace("\\u00e1", "á");
				nombre = nombre.Replace("\\u00e9", "é");
				nombre = nombre.Replace("\\u00ed", "í");
				nombre = nombre.Replace("\\u00f3", "ó");
				nombre = nombre.Replace("\\u00fa", "ú");
				nombre = nombre.Replace("\\u00c1", "Á");
				nombre = nombre.Replace("\\u00c9", "É");
				nombre = nombre.Replace("\\u00cd", "Í");
				nombre = nombre.Replace("\\u00d3", "Ó");
				nombre = nombre.Replace("\\u00da", "Ú");
				nombre = nombre.Replace("\\u00f1", "ñ");
				nombre = nombre.Replace("\\u00d1", "Ñ");

				var personaService = AgendaFactory.GetPersonaService();
				personaService.CreateIfNotExist(IdentityProviderEnum.Twitter, username, nombre);

				_formsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false);

				if (!string.IsNullOrWhiteSpace(returnUrl))
					return Redirect(returnUrl);
				return RedirectToAction("Index", "Home");
			}

			ModelState.AddModelError("", "The user name or password provided is incorrect.");

			return RedirectToAction("LogOn");
		}

        public ActionResult LogOff()
        {
            _formsService.SignOut();
            return RedirectToAction("Index", "Home");
        }
    }
}