Example #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // Code for adding admins to db
                    var roleStore   = new RoleStore <IdentityRole>(new ApplicationDbContext());
                    var roleManager = new RoleManager <IdentityRole>(roleStore);
                    await roleManager.CreateAsync(new IdentityRole("User"));

                    await UserManager.AddToRoleAsync(user.Id, "User");

                    // Uncomment to add admin
                    //await roleManager.CreateAsync(new IdentityRole("Admin"));
                    //await UserManager.AddToRoleAsync(user.Id, "Admin");
                    //////////////////////////////////////////////////////


                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);


                    // Adding user to mongodb
                    MongodbFunctions mongo = new MongodbFunctions();

                    Database.DomainModel.User newUser = new Database.DomainModel.User
                    {
                        Email   = model.Email,
                        Name    = model.FirstName,
                        Phone   = model.Phone,
                        Surname = model.Surname
                    };
                    newUser.Address.Add(model.Address);

                    mongo.InsertUser(newUser);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // Code for users to db
                    var roleStore   = new RoleStore <IdentityRole>(new ApplicationDbContext());
                    var roleManager = new RoleManager <IdentityRole>(roleStore);
                    await roleManager.CreateAsync(new IdentityRole("User"));

                    await UserManager.AddToRoleAsync(user.Id, "User");

                    // Uncomment to add admin
                    //await roleManager.CreateAsync(new IdentityRole("Admin"));
                    //await UserManager.AddToRoleAsync(user.Id, "Admin");
                    //////////////////////////////////////////////////////


                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);


                    // Adding user to mongodb
                    MongodbFunctions mongo = new MongodbFunctions();

                    Databases.DomainModel.User newUser = new Databases.DomainModel.User
                    {
                        Email     = model.Email,
                        Name      = model.FirstName,
                        BirthDate = model.BirthDate,
                        Phone     = model.Phone,
                        Surname   = model.Surname,
                        Gender    = model.Gender,
                        Interests = model.Interests.Where(x => x.Value == true).Select(x => x.Key).ToList()
                    };
                    newUser.Address.Add(model.Address);

                    mongo.InsertUser(newUser);

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }