comparison 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
comparison
equal deleted inserted replaced
99:3027c64344bd 100:cc91817a4206
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.Domain;
6 using AltNetHispano.Agendas.Factories;
7 using AltNetHispano.Agendas.Twitter;
4 using AltNetHispano.Agendas.Web.Models; 8 using AltNetHispano.Agendas.Web.Models;
9 using AltNetHispano.Agendas.Web.Services;
5 10
6 namespace AltNetHispano.Agendas.Web.Controllers 11 namespace AltNetHispano.Agendas.Web.Controllers
7 { 12 {
8 public class AccountController : Controller 13 public class AccountController : Controller
9 { 14 {
31 [HttpPost] 36 [HttpPost]
32 public ActionResult LogOn(LogOnModel model, string returnUrl) 37 public ActionResult LogOn(LogOnModel model, string returnUrl)
33 { 38 {
34 if (ModelState.IsValid) 39 if (ModelState.IsValid)
35 { 40 {
36 if (MembershipService.ValidateUser(model.UserName, model.Password)) 41 if (MembershipService.ValidateUser(model.UserName, model.Password))
37 { 42 {
38 FormsService.SignIn(model.UserName, model.RememberMe); 43 var personaService = AgendaFactory.GetPersonaService();
39 if (Url.IsLocalUrl(returnUrl)) 44 if (personaService.Validate(IdentityProviderEnum.BuiltIn, model.UserName))
40 { 45 {
41 return Redirect(returnUrl); 46 FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.BuiltIn] + model.UserName, model.RememberMe);
42 } 47 if (Url.IsLocalUrl(returnUrl))
43 else 48 return Redirect(returnUrl);
44 { 49 return RedirectToAction("Index", "Home");
45 return RedirectToAction("Index", "Home"); 50 }
46 } 51 }
47 } 52 ModelState.AddModelError("", "The user name or password provided is incorrect.");
48 else
49 {
50 ModelState.AddModelError("", "The user name or password provided is incorrect.");
51 }
52 } 53 }
53 54
54 // If we got this far, something failed, redisplay form 55 // If we got this far, something failed, redisplay form
55 return View(model); 56 return View(model);
56 } 57 }
57 58
58 // ************************************** 59 public ActionResult TwitterLogOn()
60 {
61 var oAuth = new OAuthTwitter();
62
63 if (Request["oauth_token"] == null)
64 {
65 //Redirect the user to Twitter for authorization.
66 //Using oauth_callback for local testing.
67 var action = Url.Action("TwitterLogOn");
68 var url = Request.Url.Scheme + "://" + Request.Url.Host +
69 (Request.Url.Port != 80 ? ":" + Request.Url.Port : string.Empty) + action;
70
71 oAuth.CallBackUrl = url;
72 Response.Redirect(oAuth.AuthorizationLinkGet());
73 }
74 else
75 {
76 //Get the access token and secret.
77 oAuth.AccessTokenGet(Request["oauth_token"], Request["oauth_verifier"]);
78 if (oAuth.TokenSecret.Length > 0)
79 {
80 //We now have the credentials, so make a call to the Twitter API.
81 const string url = "http://twitter.com/account/verify_credentials.xml";
82 string xml = oAuth.OAuthWebRequest(OAuthTwitter.Method.GET, url, String.Empty);
83
84 var username = GetXmlContent(xml, "screen_name");
85 var nombre = GetXmlContent(xml, "name");
86
87 var personaService = AgendaFactory.GetPersonaService();
88 if (personaService.Validate(IdentityProviderEnum.Twitter, username, nombre))
89 {
90 FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.Twitter]+username, false);
91 return RedirectToAction("Index", "Home");
92 }
93 ModelState.AddModelError("", "The user name or password provided is incorrect.");
94
95
96 //POST Test
97 //url = "http://twitter.com/statuses/update.xml";
98 //xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.POST, url, "status=" + oAuth.UrlEncode("Hello @swhitley - Testing the .NET oAuth API"));
99 //apiResponse.InnerHtml = Server.HtmlEncode(xml);
100 }
101 }
102
103 return RedirectToAction("LogOn");
104 }
105
106 private static string GetXmlContent(string xml, string tagName)
107 {
108 var tagStart = "<" + tagName + ">";
109 var tagEnd = "</" + tagName + ">";
110
111 var start = xml.IndexOf(tagStart)+tagStart.Length;
112 if (start>tagStart.Length)
113 {
114 var end = xml.IndexOf(tagEnd, start);
115 if (end>0)
116 {
117 return xml.Substring(start, end - start);
118 }
119 }
120 return string.Empty;
121 }
122
123 // **************************************
59 // URL: /Account/LogOff 124 // URL: /Account/LogOff
60 // ************************************** 125 // **************************************
61 126
62 public ActionResult LogOff() 127 public ActionResult LogOff()
63 { 128 {
84 // Attempt to register the user 149 // Attempt to register the user
85 MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email); 150 MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);
86 151
87 if (createStatus == MembershipCreateStatus.Success) 152 if (createStatus == MembershipCreateStatus.Success)
88 { 153 {
89 FormsService.SignIn(model.UserName, false /* createPersistentCookie */); 154 var personaService = AgendaFactory.GetPersonaService();
90 return RedirectToAction("Index", "Home"); 155 if (personaService.Validate(IdentityProviderEnum.BuiltIn, model.UserName, model.Nombre))
156 {
157 FormsService.SignIn(Identification.Map[(int)IdentityProviderEnum.BuiltIn] + model.UserName, false);
158 return RedirectToAction("Index", "Home");
159 }
91 } 160 }
92 else 161 ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
93 {
94 ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
95 }
96 } 162 }
97 163
98 // If we got this far, something failed, redisplay form 164 // If we got this far, something failed, redisplay form
99 ViewBag.PasswordLength = MembershipService.MinPasswordLength; 165 ViewBag.PasswordLength = MembershipService.MinPasswordLength;
100 return View(model); 166 return View(model);