PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Kona.Web/Controllers/AuthenticationController.cs

#
C# | 130 lines | 97 code | 27 blank | 6 comment | 12 complexity | c74c339b76fa45579d0d55699ea74f3f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using System.Web.Mvc.Ajax;
  7. using Kona.Infrastructure;
  8. using System.Web.Security;
  9. using Kona.Model;
  10. using System.Net;
  11. using System.Xml.Linq;
  12. namespace Kona.Web.App.Controllers
  13. {
  14. public class AuthenticationController : KonaController
  15. {
  16. IAuthenticationService _authService;
  17. ICMSRepository _cmsRepository;
  18. ICustomerRepository _customerRepository;
  19. IObjectStore _objectStore;
  20. public AuthenticationController(ICustomerRepository customerRepository,
  21. ICMSRepository cmsRepository,
  22. IObjectStore objectStore,
  23. IAuthenticationService authService) : base(customerRepository,objectStore,cmsRepository) {
  24. _authService = authService;
  25. _cmsRepository = cmsRepository;
  26. _objectStore = objectStore;
  27. _customerRepository = customerRepository;
  28. }
  29. public ActionResult Login() {
  30. string login = Request.Form["username"];
  31. string password = Request.Form["password"];
  32. string remember = Request.Form["rememberMe"];
  33. bool setCookie = false;
  34. if (!string.IsNullOrEmpty(remember))
  35. setCookie = remember == "true";
  36. if (!String.IsNullOrEmpty(login) && !String.IsNullOrEmpty(password)) {
  37. bool isValid = _authService.IsValidLogin(login, password);
  38. //log them in
  39. if (isValid) {
  40. if (setCookie)
  41. FormsAuthentication.SetAuthCookie(login, true);
  42. return AuthAndRedirect(login, login);
  43. }
  44. }
  45. return View();
  46. }
  47. public ActionResult ReceiveToken(string token) {
  48. //you'll want to replace this with your own :)
  49. string parameters = String.Format("apiKey={0}&token={1}&format=xml", this.SiteData.RPXAPIKey, token);
  50. string response;
  51. using (var w = new WebClient()) {
  52. response = w.UploadString("https://rpxnow.com/api/v2/auth_info", parameters);
  53. }
  54. var xmlResponse = XDocument.Parse(response);
  55. var userProfile = (from x in xmlResponse.Descendants("profile")
  56. select new
  57. {
  58. id = x.Element("identifier").Value,
  59. userName = (string)x.Element("preferredUsername") ?? "No Preferred Username",
  60. displayname = (string)x.Element("displayName") ?? "No Display Name",
  61. email = (string)x.Element("email") ?? "No Email"
  62. }).SingleOrDefault();
  63. //We store the unique ID for that user in the FormsAuth additional data.
  64. // That's the Users GUID, for all intents.
  65. //hopefully we have something...
  66. if (userProfile != null) {
  67. string friendly=userProfile.userName;
  68. if (!string.IsNullOrEmpty(userProfile.displayname))
  69. friendly = userProfile.displayname;
  70. //We store the unique ID for that user in the FormsAuth additional data.
  71. return AuthAndRedirect(userProfile.id, friendly);
  72. } else {
  73. return RedirectToAction("Index", "Home");
  74. }
  75. }
  76. ActionResult AuthAndRedirect(string userName, string friendlyName) {
  77. string returnUrl = Request["ReturnUrl"];
  78. SetCookies(userName, friendlyName);
  79. if (!String.IsNullOrEmpty(returnUrl)) {
  80. return Redirect(returnUrl);
  81. } else {
  82. return RedirectToAction("Index", "Home");
  83. }
  84. }
  85. void SetCookies(string userName, string friendlyName) {
  86. Response.Cookies["shopper"].Value = userName;
  87. Response.Cookies["shopper"].Expires = DateTime.Now.AddDays(30);
  88. Response.Cookies["shopper"].HttpOnly = true;
  89. Response.Cookies["shopperName"].Value = friendlyName;
  90. Response.Cookies["shopperName"].Expires = DateTime.Now.AddDays(30);
  91. Response.Cookies["shopperName"].HttpOnly = true;
  92. FormsAuthentication.SetAuthCookie(userName, true);
  93. }
  94. public ActionResult Logout() {
  95. Response.Cookies["shopper"].Value = null;
  96. Response.Cookies["shopper"].Expires = DateTime.Now.AddDays(-1);
  97. Response.Cookies["shopperName"].Value = null;
  98. Response.Cookies["shopperName"].Expires = DateTime.Now.AddDays(-1);
  99. FormsAuthentication.SignOut();
  100. return RedirectToAction("Index", "Home");
  101. }
  102. }
  103. }