Esempio n. 1
0
        public ActionResult AddUserToRole(string Id, string Name)
        {
            ViewBag.Users = new SelectList(con.Users, "Id", "Email");
            ViewBag.Roles = new SelectList(con.appRoles, "Name", "Name");

            if (Id != "" && Name != null)
            {
                if (rb.IsUserInRole(Id, Name) == false)
                {
                    rb.AddUserToRole(Id, Name);
                    ViewBag.Result = "User successfully assigned a role!";
                }

                else
                {
                    ViewBag.Result = "User is already in selected Role!";
                }
            }

            else
            {
                ViewBag.Result = "Please select Username and Rolename!";
            }

            return(View());
        }
Esempio n. 2
0
        public async Task <ActionResult> RegisterCustomer(RegisterViewModel model)
        {
            RolesBusiness     rb = new RolesBusiness();
            Customer_Business cb = new Customer_Business();

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, Full_Name = model.FirstName + " " + model.LastName, PhoneNumber = model.phone, EmailConfirmed = true
                };

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

                if (result.Succeeded)
                {
                    try
                    {
                        ApplicationDbContext db = new ApplicationDbContext();
                        rb.AddUserToRole(db.Users.FirstOrDefault(x => x.UserName == model.Email).Id, WebConstants.CustomerRole);

                        var customer = new Customer();
                        {
                            customer.FirstName = user.Full_Name;
                            customer.LastName  = "df";
                            customer.Email     = user.Email;
                            customer.phone     = user.PhoneNumber;
                            customer.address   = model.address;

                            var isSave = cb.add(customer);
                            db.SaveChanges();
                        };
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError(string.Empty, "Unable to create customer Profile.");
                    }
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 3
0
        public async Task <ActionResult> Create(CustomerCreateVM model)
        {
            RolesBusiness rb = new RolesBusiness();

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, PhoneNumber = model.phone, EmailConfirmed = true
                };
                var adminresult = await UserManager.CreateAsync(user, model.Password);

                //cb.add(model);

                if (adminresult.Succeeded)
                {
                    ApplicationDbContext db = new ApplicationDbContext();
                    rb.AddUserToRole(db.Users.FirstOrDefault(x => x.UserName == model.Email).Id, WebConstants.CustomerRole);

                    // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, protocol: Request.Url.Scheme);
                    var client      = new SendGridClient("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");
                    var from        = new EmailAddress("*****@*****.**", "MontClair Veterinary");
                    var subject     = "Confirm your account";
                    var to          = new EmailAddress(model.Email, model.FirstName + " " + model.LastName);
                    var htmlContent = "Hello " + model.FirstName + " " + model.LastName + ", Kindly confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>";
                    var msg         = MailHelper.CreateSingleEmail(from, to, subject, null, htmlContent);
                    var response    = client.SendEmailAsync(msg);
                }

                //Success(string.Format("<b>{0}</b> was successfully added to the database.", model.FirstName), true);
                return(RedirectToAction("Create"));
            }

            return(View(model));
        }