view Agendas/trunk/src/Agendas.Web/Controllers/AccountController.cs @ 279:1e889a2e45c5

#125: ABM de patrocinadores.
author juanjose.montesdeocaarbos
date Thu, 22 Dec 2011 10:04:08 -0300
parents 72a96459f910
children
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");

				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");
        }
    }
}