예제 #1
0
 public ActionResult Index(UserViewModel viewModel, string returnUrl)
 {
     try {
         if (!string.IsNullOrEmpty(viewModel.User.Email) && !string.IsNullOrEmpty(viewModel.User.Password))
         {
             User user = dal.authentifier(viewModel.User.Email, viewModel.User.Password);
             if (user != null)
             {
                 FormsAuthentication.SetAuthCookie(user.Id.ToString(), false);
                 if (!string.IsNullOrWhiteSpace(returnUrl) && Url.IsLocalUrl(returnUrl))
                 {
                     return Redirect(returnUrl);
                 }
             }
         }
         throw new Exception("Wrong login");
     }
     catch (Exception ex)
     {
         Log.Error(string.Format("Controller = {0}, Action={1}, Email={2}, Exception={3}",
             this.ControllerContext.RouteData.Values["controller"].ToString(),
             this.ControllerContext.RouteData.Values["action"].ToString(),
             viewModel.User.Email,
             ex.Message));
         ModelState.AddModelError("User.Email", ex.Message);
         return View(viewModel);
     }
 }
예제 #2
0
 public UserViewModel getUserViewModel()
 {
     int idUser;
     UserViewModel viewModel = new UserViewModel{ Authentifie = HttpContext.User.Identity.IsAuthenticated };
     if (HttpContext.User.Identity.IsAuthenticated)
     {
         if (int.TryParse(HttpContext.User.Identity.Name, out idUser))
         {
             viewModel.User = dal.getUser(idUser);
         }
     }
     return viewModel;
 }
예제 #3
0
 public ActionResult Index()
 {
     int idUser;
     UserViewModel viewModel = new UserViewModel { Authentifie = HttpContext.User.Identity.IsAuthenticated };
     if (HttpContext.User.Identity.IsAuthenticated)
     {
         if (int.TryParse(HttpContext.User.Identity.Name, out idUser))
         {
             viewModel.User = dal.getUser(idUser);
         }
     }
     return View(viewModel);
 }
 //
 // GET: /EmailValidation/Id
 public ActionResult Index(string id)
 {
     User user = dal.getUserPerEmailHash(id);
     if (user != null)
     {
         dal.validateUserEmail(user);
         FormsAuthentication.SetAuthCookie(user.Id.ToString(), false);
         ViewBag.successMessage = "Email validation OK";
     }
     else
     {
         ViewBag.errorMessage = "Wrong Email link";
     }
     UserViewModel viewModel = new UserViewModel();
     viewModel.User = user;
     return View(viewModel);
 }
예제 #5
0
 public ActionResult CreateAccount(User user)
 {
     UserViewModel viewModel = new UserViewModel();
     viewModel.User = user;
     try {
         if (ModelState.IsValid)
         {
             if (!Regex.IsMatch(user.Email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"))
             {
                 throw new Exception("Wrong email format");
             }
             else
             {
                 if (dal.getUser(user.Email) != null)
                 {
                     throw new Exception("Email already register");
                 }
                 else
                 {
                     user = dal.addUser(user.Email, user.Name, user.Password);
                     try
                     {
                         string urlEmailValidation = Request.Url.GetLeftPart(UriPartial.Authority) + "/EmailValidation/Index/" + user.EmailHash;
                         StringBuilder body = new StringBuilder();
                         body.Append("<div style='background-color:#eee; width:100%; height:100%; font-size:20px; font-family: Calibri, Arial;text-align:center;'>");
                         body.Append(String.Concat("Hello ", user.Name));
                         body.Append("<br>");
                         body.Append("To validate your email, please follow this <a href='");
                         body.Append(urlEmailValidation);
                         body.Append("'>link</a>");
                         body.Append("</div>");
                         Email.send(user, "Fridge - Email Validation", body.ToString());
                         //update user for success Page
                         viewModel.User = user;
                         //save changes in BDD
                         dal.saveChanges();
                     }
                     catch (Exception ex)
                     {
                         throw new Exception("Error Email Sending", ex);
                     }
                 }
             }
         }
         else
         {
             throw new Exception("Missing Field");
         }
         return View(viewModel);
     }
     catch (Exception ex)
     {
         Log.Error(string.Format("Controller = {0}, Action={1}, Email={2}, Exception={3}",
             this.ControllerContext.RouteData.Values["controller"].ToString(),
             this.ControllerContext.RouteData.Values["action"].ToString(),
             user.Email,
             (ex.InnerException != null ? ex.InnerException.ToString() : ex.Message)));
         ModelState.AddModelError("User.Email", ex.Message);
         return View(viewModel);
     }
 }