Beispiel #1
0
        public async Task <IActionResult> ConfirmEmail(string userId, string token)
        {
            if (userId == null || token == null)
            {
                return(RedirectToAction("index", "Home"));
            }

            var user = await Usermanager.FindByIdAsync(userId);

            if (user == null)
            {
                ViewBag.ErrorMessage = $"The User ID {userId} is invalid";
                return(View("NotFound"));
            }

            var result = await Usermanager.ConfirmEmailAsync(user, token);

            if (result.Succeeded)
            {
                string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Welcome.cshtml", user.FullName);

                await _emailSender.SendEmailAsync(user.Email, "Welcome To Mobile Store", str);

                return(View("EmailConfirmed"));
            }

            ViewBag.ErrorTitle = "Email cannot be confirmed";
            return(View("Error"));
        }
Beispiel #2
0
        public async Task <ActionResult> Register([Bind(Include = "ProfilePic,UserName,Email,Password,ConfirmPassword,Discord,Twitch,Btag")] RegisterViewModel people)
        {
            try
            {
                //No more than 5 MB
                if (ModelState.IsValid && people.ProfilePic.ContentLength > 0 && people.ProfilePic.ContentLength < 5242880)
                {
                    People p = new People
                    {
                        UserName = people.UserName,
                        Email    = people.Email,
                    };
                    string _path = Path.Combine(Server.MapPath("~/UserData/ProfilePictures"), p.Id);
                    people.ProfilePic.SaveAs(_path);
                    if (System.IO.File.Exists(_path))
                    {
                        p.ProfilePicLoc = _path;
                        IdentityResult result = await UserManager.CreateAsync(p, people.Password);

                        if (result.Succeeded)
                        {
                            //await SignInManager.SignInAsync(p, isPersistent: false, rememberBrowser: false);
                            //return RedirectToAction("Index", "Home");

                            var code = await UserManager.GenerateEmailConfirmationTokenAsync(p.Id);

                            var callbackUrl = Url.Action(
                                "ConfirmEmail", "Account",
                                new { userId = p.Id, code = code },
                                protocol: Request.Url.Scheme);
                            var emailModel = new EmailAskForConfirmationViewModel
                            {
                                Name = p.UserName,
                                Link = callbackUrl
                            };
                            var emailString = ViewToStringRenderer.RenderViewToString(this.ControllerContext, "~/MailTemplates/ConfirmEmailTemplate.cshtml", emailModel);
                            await UserManager.SendEmailAsync(p.Id,
                                                             "Confirm your account", emailString);

                            return(View("DisplayEmail"));
                        }
                        AddErrors(result);
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return(View(people));
        }
Beispiel #3
0
        public async Task <IActionResult> ForgotPassword(string Email)
        {
            if (Email == null)
            {
                ModelState.AddModelError("", "Email Can't be null");
                return(View());
            }
            if (ModelState.IsValid)
            {
                // Find the user by email
                var user = await Usermanager.FindByEmailAsync(Email);

                // If the user is found AND Email is confirmed
                if (user != null && await Usermanager.IsEmailConfirmedAsync(user))
                {
                    // Generate the reset password token
                    var token = await Usermanager.GeneratePasswordResetTokenAsync(user);

                    // Build the password reset link
                    var passwordResetLink = Url.Action("ResetPassword", "Account",
                                                       new { email = Email, token = token }, Request.Scheme);


                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/ResetPassword.cshtml", passwordResetLink);

                    //util.sendemail(user.Email, "Reset Account Password", str);
                    //await _emailSender.SendEmailAsync(user.Email, "Reset Account Password", $"<h2>Here is the Reset Password Confirmation Link</h2></br> <a href ={passwordResetLink}>{passwordResetLink}</a>");
                    await _emailSender.SendEmailAsync(user.Email, "Reset Account Password", str);


                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Password Reset Success";
                    ViewBag.Message   = "Before you can Login, please Reset your " +
                                        "Password, by clicking on the Reset Password link we have emailed you";
                    return(View("EmailConfirmation"));
                }

                // To avoid account enumeration and brute force attacks, don't
                // reveal that the user does not exist or is not confirmed
                ViewBag.PageTitle = "Email Confirmation";
                ViewBag.Title     = "Password Reset Success";
                ViewBag.Message   = "Before you can Login, please Reset your " +
                                    "Password, by clicking on the Reset Password link we have emailed you";
                return(View("EmailConfirmation"));
            }

            return(View());
        }
        public async Task <IActionResult> AddEmployee(RegisterEmployeeViewModel model)
        {
            if (ModelState.IsValid)

            {
                var user = new ApplicationUser
                {
                    FullName     = model.FullName,
                    UserName     = model.Email,
                    Email        = model.Email,
                    City         = model.cityId,
                    PhoneNumber  = model.PhoneNumber,
                    StreetAdress = model.StreetAdress,
                    store_id     = model.store_id,
                    isactive     = true,
                    Photopath    = util.ProcessPhotoproperty(model.Photo)
                };

                var LoginUser = await Usermanager.GetUserAsync(User);

                user.addedBy = LoginUser.Id;


                var result = await Usermanager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    foreach (var role in model.Roles.Where(x => x.isSelected))
                    {
                        if (User.IsInRole("Admin") && role.RoleName == "Admin" && role.RoleName == "Super Admin")
                        {
                            await Usermanager.DeleteAsync(user);

                            return(Forbid());
                        }
                    }


                    var roles = await Usermanager.AddToRolesAsync(user, model.Roles.Where(x => x.isSelected).Select(y => y.RoleName));

                    if (!roles.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add selected roles to user! Try Again");
                        await Usermanager.DeleteAsync(user);

                        return(View(model));
                    }
                    // Add all the claims that are selected on the UI
                    var claims = await Usermanager.AddClaimsAsync(user,
                                                                  model.Claims.Select(c => new Claim(c.ClaimType, c.isSelected ? "true" : "false")));

                    if (!claims.Succeeded)
                    {
                        ModelState.AddModelError("", "Cannot add selected claims to user!  Edit User and Insert Claims from there");
                        return(View(model));
                    }
                    var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink);

                    await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str);

                    //util.sendemail(user.Email, "Email Confirmation", $"<h2>Here is the Confirmation Link</h2></br> <a href={confirmationLink}>{confirmationLink}</a>");
                    if (Signinmanager.IsSignedIn(User) && (User.IsInRole("Admin") || User.IsInRole("Super Admin")))
                    {
                        return(RedirectToAction("ListUsers", "Administration"));
                    }
                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Registration successful";
                    ViewBag.Message   = "Before you can Login, please confirm your " +
                                        "email, by clicking on the confirmation link we have emailed you";
                    return(View("EmailConfirmation"));

                    // await Signinmanager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                ViewBag.Stores = util.GetAllStores();
                ViewBag.cities = util.getCities();
                return(View(model));
            }
            ViewBag.cities = util.getCities();
            ViewBag.Stores = util.GetAllStores();
            return(View(model));
        }
Beispiel #5
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var LoginUser = await Usermanager.GetUserAsync(User);

                var user = new ApplicationUser
                {
                    FullName     = model.FullName,
                    UserName     = model.Email,
                    Email        = model.Email,
                    City         = model.cityId,
                    PhoneNumber  = model.PhoneNumber,
                    StreetAdress = model.StreetAdress,
                    Photopath    = util.ProcessPhotoproperty(model.Photo),
                    isactive     = true
                };
                if (Signinmanager.IsSignedIn(User) && User.IsInRole("Employee"))
                {
                    if (!User.HasClaim(claim => claim.Type == "Create User" && claim.Value == "true"))
                    {
                        return(Forbid());
                    }
                    user.store_id = LoginUser.store_id;
                    user.addedBy  = LoginUser.Id;
                }

                var result = await Usermanager.CreateAsync(user, model.Password);

                var roles = await Usermanager.AddToRoleAsync(user, "User");


                if (result.Succeeded)
                {
                    var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user);

                    var confirmationLink = Url.Action("ConfirmEmail", "Account",
                                                      new { userId = user.Id, token = token }, Request.Scheme);

                    string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink);

                    await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str);

                    ViewBag.PageTitle = "Email Confirmation";
                    ViewBag.Title     = "Registration successful";
                    ViewBag.Message   = "Before you can Login, please confirm your " +
                                        "email, by clicking on the confirmation link we have emailed you";
                    return(View("EmailConfirmation"));

                    // await Signinmanager.SignInAsync(user, isPersistent: false);
                    //return RedirectToAction("Index", "Home");
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
                ViewBag.Stores = util.GetAllStores();
                ViewBag.cities = util.getCities();
                return(View(model));
            }
            ViewBag.Stores = util.GetAllStores();
            ViewBag.cities = util.getCities();
            return(View(model));
        }
        public async Task <ActionResult> Create(OrderViewModel c)
        {
            try
            {
                if (c.orders == "[]" || c.Products.Count == 0)
                {
                    ViewBag.Companies = util.GetAllCompany();
                    ViewBag.Stores    = util.GetAllStores();
                    ViewBag.cities    = util.getCities();
                    ModelState.AddModelError("", "Products Can't be Null");
                    return(View(c));
                }
                if (c.Products.Any(x => x.Quantity <= 0))
                {
                    ViewBag.Companies = util.GetAllCompany();
                    ViewBag.Stores    = util.GetAllStores();
                    ViewBag.cities    = util.getCities();
                    ModelState.AddModelError("", "Products Can't be Negative");
                    return(View(c));
                }
                // TODO: Add insert logic here
                if (ModelState.IsValid)
                {
                    var user = await userManager.GetUserAsync(User);

                    if (!(User.IsInRole("Admin") || User.IsInRole("Super Admin") || User.IsInRole("Employee")))
                    {
                        c.cus_name    = user.FullName;
                        c.cus_phone   = user.PhoneNumber;
                        c.orderStatus = Status.Pending;
                        if (user.CusRef != null)
                        {
                            if (c.CustRef != user.CusRef)
                            {
                                c.CustRef = (int)user.CusRef;
                            }
                        }
                        else
                        {
                            c.CustRef = util.GenerateCusRef();
                        }
                        user.CusRef = c.CustRef;
                        await userManager.UpdateAsync(user);
                    }
                    else
                    {
                        c.orderStatus = Status.Completed;
                        c.TakenBy     = user.Id;
                    }

                    List <Tuple <int, bool> > res = await iOrderRepositery.addOrder(c, Url);

                    if (res.Any(x => x.Item2 == false && x.Item1 > 0))
                    {
                        ViewBag.Companies = util.GetAllCompany();
                        ViewBag.Stores    = util.GetAllStores();
                        ViewBag.cities    = util.getCities();
                        var    OutOfStock   = res.Where(x => x.Item2 == false);
                        var    listOfModels = c.Products.Where(x => OutOfStock.Select(i => i.Item1).Contains(x.modelId)).Select(x => new { x.model_name, x.com_Name });
                        string str          = "";
                        foreach (var item in listOfModels)
                        {
                            str = str + item.com_Name + " " + item.model_name + "\n";
                        }
                        ModelState.AddModelError("", "Following Product(s) is Out of Stock /n" + String.Join("/n", listOfModels));
                        return(View(c));
                    }
                    else if (res.Any(x => x.Item2 == false && x.Item1 == -1))
                    {
                        ViewBag.Companies = util.GetAllCompany();
                        ViewBag.Stores    = util.GetAllStores();
                        ViewBag.cities    = util.getCities();
                        ModelState.AddModelError("", "Order Placed! Error in Updating Stock");
                        return(View(c));
                    }
                    else if (res.Any(x => x.Item2 == false && x.Item1 == -2))
                    {
                        ViewBag.Companies = util.GetAllCompany();
                        ViewBag.Stores    = util.GetAllStores();
                        ViewBag.cities    = util.getCities();
                        ModelState.AddModelError("", "Error in Payment Gatway");
                        return(View(c));
                    }

                    var id = res.Select(x => x.Item1).First();
                    if (!(User.IsInRole("Admin") || User.IsInRole("Super Admin") || User.IsInRole("Employee")))
                    {
                        c.order_id = id;
                        string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/OrderConfirmation.cshtml", c);

                        await _emailSender.SendEmailAsync(user.Email, "Order Confirmation", str);
                    }
                    string ID = protector.Protect(id.ToString());
                    return(RedirectToAction("Details", new { id = ID }));
                }
                ViewBag.Companies = util.GetAllCompany();
                ViewBag.Stores    = util.GetAllStores();
                ViewBag.cities    = util.getCities();
                return(View(c));
            }
            catch (Exception e)
            {
                ViewBag.Companies = util.GetAllCompany();
                ViewBag.Stores    = util.GetAllStores();
                ViewBag.cities    = util.getCities();
                ModelState.AddModelError("", e.Message);
                return(View(c));
            }
        }