Ejemplo n.º 1
0
        public ActionResult LogIn(UserLogin login, string ReturnUrl = "")
        {
            string message = "";

            using (UserRegistrationDBEntities _db = new UserRegistrationDBEntities())
            {
                var data = _db.ApplicationUsers.Where(a => a.EmailID == login.EmailID).FirstOrDefault();
                if (data != null)
                {
                    if (string.Compare(GetHash.GetHashForString(login.Password), data.Password) == 0)
                    {
                        var    ticket    = new FormsAuthenticationTicket(login.EmailID, true, 10);
                        string encrypted = FormsAuthentication.Encrypt(ticket);
                        var    cookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
                        cookie.Expires  = DateTime.Now.AddMinutes(10);
                        cookie.HttpOnly = true;
                        Response.Cookies.Add(cookie);
                        Session["FullName"] = data.UserFirstName + " " + data.UserFamilyName;
                        Session["Email"]    = data.EmailID;
                        Session["idUser"]   = data.ApplicationUserId;

                        if (Url.IsLocalUrl(ReturnUrl))
                        {
                            return(Redirect(ReturnUrl));
                        }
                        else
                        {
                            return(RedirectToAction("ViewUserInformation", new RouteValueDictionary(
                                                        new { controller = "User", action = "UserData", Id = data.ApplicationUserId })));
                        }
                    }
                    else
                    {
                        message = "Invalid credential provided";
                    }
                }
                else
                {
                    message = "Invalid credential provided";
                }
            }
            ViewBag.Message = message;
            return(View());
        }
 public ActionResult Register(ApplicationUser _user)
 {
     if (ModelState.IsValid)
     {
         using (UserRegistrationDBEntities _db = new UserRegistrationDBEntities())
         {
             var             userCheck = _db.ApplicationUsers.FirstOrDefault(x => x.EmailID == _user.EmailID);
             ApplicationUser userData  = new ApplicationUser();
             if (userCheck == null)
             {
                 _user.Password = GetHash.GetHashForString(_user.Password);
                 _dbContext.Configuration.ValidateOnSaveEnabled = false;
                 var storeData = new ApplicationUser()
                 {
                     UserFirstName  = _user.UserFirstName,
                     UserFamilyName = _user.UserFamilyName,
                     EmailID        = _user.EmailID,
                     Address1       = _user.Address1,
                     Address2       = _user.Address2,
                     Address3       = _user.Address3,
                     Password       = _user.Password,
                     City           = _user.City,
                     State          = _user.State,
                     ZipCode        = _user.ZipCode
                 };
                 _dbContext.ApplicationUsers.Add(storeData);
                 _dbContext.SaveChanges();
                 return(RedirectToAction("ViewUserInformation", new RouteValueDictionary(
                                             new { controller = "User", action = "UserData", Id = storeData.ApplicationUserId })));
             }
             else
             {
                 ViewBag.error = "Email already exists";
                 return(View());
             }
         }
     }
     return(View());
 }