view Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 162:557c386fcecc

Ticket 153: UI de Publicar.
author juanjose.montesdeocaarbos
date Fri, 05 Aug 2011 22:02:23 -0300
parents 62dc9fb3a03e
children 3c70b0d8bd69
line wrap: on
line source

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
{
    public class AccountController : Controller
    {

        public IFormsAuthenticationService FormsService { get; set; }

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

            base.Initialize(requestContext);
        }

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

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

			if (Request["oauth_token"] == null)
			{
				var action = Url.Action("TwitterLogOn");
				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");

				var personaService = AgendaFactory.GetPersonaService();
				personaService.CreateIfNotExist(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.");

			return RedirectToAction("LogOn");
		}

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