示例#1
0
        public async Task <ActionResult> RegisterUser(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // check to see if this user is in the existing database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        var oldUser = usrMgr.AuthenticateUser(model.Email, model.Password);
                        var user    = new ApplicationUser
                        {
                            GivenName  = model.GivenName,
                            FamilyName = model.FamilyName,
                            UserName   = model.Email,
                            Email      = model.Email
                        };
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            string role = oldUser.Role;

                            UserManager.AddToRole(user.Id, role);

                            // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                            return(RedirectToAction("Index", "Admin"));
                        }
                        AddErrors(result);
                    }
                    else // not an existing user, create a user without roles
                    {
                        var user = new ApplicationUser
                        {
                            GivenName  = model.GivenName,
                            FamilyName = model.FamilyName,
                            UserName   = model.Email,
                            Email      = model.Email
                        };
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                            return(RedirectToAction("Index", "Admin"));
                        }
                        AddErrors(result);
                    }
                }
                catch
                {
                    // creating old user failed
                    return(View(model));
                }
            }
            // modelstate was not valid
            return(View(model));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // check to see if this user is in the existing user database
                    var usrMgr = new LogicLayer.UserManager();
                    try
                    {
                        var oldUser = usrMgr.AuthenticateUser(model.Email, model.Password);
                        foreach (var role in oldUser.Roles)
                        {
                            UserManager.AddToRole(user.Id, role.RoleID);
                        }
                    }
                    catch (Exception)
                    {
                        // what to do if the user is not an existing user
                    }
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://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));
        }
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // check to see if this user is in the existing database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        // this requires the user to use the same password as the one in the internal database
                        var oldUser = usrMgr.AuthenticateUser(model.Email, model.Password);
                        var user    = new ApplicationUser
                        {
                            // populate these fields with wxisting data from oldUser
                            GivenName  = oldUser.FirstName,
                            FamilyName = oldUser.LastName,
                            PersonID   = oldUser.PersonID,

                            // populate these fields normally
                            UserName = model.Email,
                            Email    = model.Email
                        };
                        // create the user with the Identity system UserManager normally
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            // use the oldUser.Roles list to add the internally assigned roles to the user
                            foreach (var role in oldUser.Roles)
                            {
                                UserManager.AddToRole(user.Id, role);
                            }
                            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(result);
                    }
                    else // not an existing user, create a user withou
                    {
                        var user = new ApplicationUser
                        {
                            // We will uncomment the following two lines later, once our ViewModel and
                            // our View are updated to ask for them:
                            GivenName  = model.GivenName,
                            FamilyName = model.FamilyName,
                            UserName   = model.Email,
                            Email      = model.Email
                        };
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            // my code
                            var dbUser = new DataObjects.User
                            {
                                FirstName = model.GivenName,
                                LastName  = model.FamilyName,
                                Email     = model.Email
                            };
                            usrMgr.AddNewlyRegisteredUser(dbUser, model.Password);


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

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(result);
                    }
                }
                catch
                {
                    // creating old user failed, probably because AuthenticateUser failed
                    return(View(model));
                }
            }
            // modelstate was not valid
            return(View(model));
        }
示例#4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Check to see if this user is in the exising database
                LogicLayer.UserManager usrMgr = new LogicLayer.UserManager();
                try
                {
                    if (usrMgr.FindUser(model.Email))
                    {
                        // This requires the user to use the same password as the one in the internal database.
                        var oldUser = usrMgr.AuthenticateUser(model.Email, model.Password);
                        var user    = new ApplicationUser
                        {
                            // Populate thes fields with exising data from oldUser
                            GivenName  = oldUser.FirstName,
                            FamilyName = oldUser.LastName,
                            EmployeeID = oldUser.EmployeeID,

                            // Populate these fields normally
                            UserName = model.Email,
                            Email    = model.Email
                        };
                        // Create the user with the Identity system UserManager normally.
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            // Use the oldUser.Roles list to add the internally assigned roles to the user.
                            foreach (var role in oldUser.Roles)
                            {
                                UserManager.AddToRole(user.Id, role);
                            }
                            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(result);
                    }
                    // Not an existing user, create a user without roles.
                    else
                    {
                        var user = new ApplicationUser
                        {
                            GivenName  = model.GivenName,
                            FamilyName = model.FamilyName,
                            UserName   = model.Email,
                            Email      = model.Email
                        };
                        var result = await UserManager.CreateAsync(user, model.Password);

                        if (result.Succeeded)
                        {
                            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                            return(RedirectToAction("Index", "Home"));
                        }
                        AddErrors(result);
                    }
                }
                catch
                {
                    // Creating old user failed, probably because AuthenticateUser failed
                    return(View(model));
                }
            }

            // ModelState was not valid
            return(View(model));
        }