The next generation of the Teknik Services. Written in ASP.NET.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProfileController.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.Security;
  7. using Teknik.Areas.Profile.ViewModels;
  8. using Teknik.Controllers;
  9. using Teknik.Models;
  10. using Teknik.ViewModels;
  11. namespace Teknik.Areas.Profile.Controllers
  12. {
  13. public class ProfileController : DefaultController
  14. {
  15. private TeknikEntities db = new TeknikEntities();
  16. // GET: Profile/Profile
  17. public ActionResult Index()
  18. {
  19. ViewBag.Title = Config.Title + " - Profile";
  20. ViewBag.Message = "View Your Profile";
  21. return View();
  22. }
  23. [HttpGet]
  24. [AllowAnonymous]
  25. // GET: Profile
  26. public ActionResult Login()
  27. {
  28. return View();
  29. }
  30. [HttpPost]
  31. [AllowAnonymous]
  32. public ActionResult Login(LoginViewModel model)
  33. {
  34. if (ModelState.IsValid)
  35. {
  36. if (model.IsValid())
  37. {
  38. FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
  39. return Json(new { result = "true" });
  40. }
  41. }
  42. return Json(new { error = "Invalid User name or Password." });
  43. }
  44. public ActionResult Logout()
  45. {
  46. FormsAuthentication.SignOut();
  47. return RedirectToAction("Index", "Home", new { Area = "Home" });
  48. }
  49. [HttpPost]
  50. [AllowAnonymous]
  51. public ActionResult Register(RegisterViewModel model)
  52. {
  53. if (ModelState.IsValid)
  54. {
  55. var foundUser = db.Users.Where(b => b.Username == model.Username);
  56. if (foundUser.Any())
  57. {
  58. return Json(new { error = "That username already exists." });
  59. }
  60. if (model.Password != model.ConfirmPassword)
  61. {
  62. return Json(new { error = "Passwords must match." });
  63. }
  64. if (model.Insert())
  65. {
  66. return RedirectToAction("Login", "Profile", new LoginViewModel { Username = model.Username, Password = model.Password });
  67. }
  68. return Json(new { error = "You must include all fields." });
  69. }
  70. return Json(new { error = "You must include all fields." });
  71. }
  72. }
  73. }