public async Task<ActionResult> CreateUser(CreateUserModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model.Email,
                    Email = model.Email,
                    PhoneNumber = model.PhoneNumber
                };

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

                if (result.Succeeded)
                {
                    result = await UserManager.AddToRolesAsync(user.Id, model.SelectedRoles);

                    if (result.Succeeded)
                    {
                        return RedirectToAction("Users");
                    }
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }

            model.AvailableRoles = RoleManager.Roles;
            return View(model);
        }
        public async Task<ActionResult> Register(RegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            var user = await UserManager.FindByNameAsync(model.UserName);

            if (user == null)
            {
                user = new ApplicationUser
                {
                    UserName = model.UserName,
                    Email = model.UserName
                };

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

                if (result.Succeeded)
                {
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);

                    if (Request.Url != null)
                    {
                        ViewBag.Message = "A confirmation email is going to arrive in your inbox shortly!";
                        var confirmationUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, Request.Url.Scheme);
                        await UserManager.SendEmailAsync(user.Id, "Please Confirm you email", "Please confirm your account by clicking this link: <a href=\"" + confirmationUrl + "\">link</a>");
                    }

                    await UserManager.AddToRoleAsync(user.Id, "User");
                }
                else
                {
                    AddErrors(result);
                    ViewBag.Message = "We are sorry, but an error occured while creating your account.";
                }
            }
            else
            {
                ViewBag.Message = "There is already an account using the email " + user.Email + ".";
            }

            return View();
        }
        private async Task StoreFacebookAuthenticationToken(ApplicationUser user)
        {
            var claimsIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

            if (claimsIdentity != null)
            {
                var currentClaims = await UserManager.GetClaimsAsync(user.Id);
                var facebookAccessToken = claimsIdentity.FindAll("FacebookAccessToken").First();

                if (!currentClaims.Any())
                {
                    await UserManager.AddClaimAsync(user.Id, facebookAccessToken);
                }
            }
        }
        public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                ViewBag.Message = "You have to provide permission to the application in order to log in using a social provider.";
                return RedirectToAction("Login");
            }

            var result = await SignInManager.ExternalSignInAsync(loginInfo, false);

            switch (result)
            {
                case SignInStatus.LockedOut:
                    ViewBag.Message = "Your account is locked out. Please contact administrator.";
                    return View("Login");
                case SignInStatus.RequiresVerification:
                    ViewBag.Message = "You need to verify your account before proceeding.";
                    return View("Login");
                case SignInStatus.Success:
                    return RedirectToLocal(returnUrl);
                // ReSharper disable once RedundantCaseLabel
                case SignInStatus.Failure:
                default:
                    var user = await UserManager.FindByNameAsync(loginInfo.Email);

                    if (user == null)
                    {
                        user = new ApplicationUser
                        {
                            UserName = loginInfo.Email,
                            Email = loginInfo.Email,
                            EmailConfirmed = true
                        };

                        var createResult = await UserManager.CreateAsync(user);

                        if (!createResult.Succeeded)
                        {
                            return RedirectToAction("Login", "Account");
                        }

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

                        await UserManager.AddLoginAsync(user.Id, loginInfo.Login);

                        if (loginInfo.Login.LoginProvider == "Facebook")
                        {
                            await StoreFacebookAuthenticationToken(user);
                        }

                        await SignInManager.SignInAsync(user, false, false);
                        return RedirectToAction("Index", "Home");
                    }

                    ViewBag.Message = "There is already an account using the email " + user.Email + ".";
                    return View("Login");
            }
        }