Inheritance: IdentityUser
        public async Task<IdentityResult> RegisterUser(BlogRegisterModel userModel)
        {
            var user = new BlogUser
            {
                UserName = userModel.Username,
                Email = userModel.Email
            };
 
            var result = await _userManager.CreateAsync(user, userModel.Password);

            return result;
        }
        public async Task<ActionResult> Register(BlogRegisterModel model)
        {
            if (!ModelState.IsValid) return View(model);

            var user = new BlogUser { UserName = model.Username, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);

            //Add User to the selected Roles 
            if (result.Succeeded)
            {
                var roleResult = await UserManager.AddToRolesAsync(user.Id, new[] { "Bloggity Staff" });
                if (!roleResult.Succeeded)
                {
                    AddErrors(roleResult);
                    return View();
                }

                var blogUser = await _userHelper.AddBlogUser(model);
                if (blogUser.Error != null)
                {
                    ViewBag.UserCreationError = blogUser.Error.Message;
                    return View(model);
                }

                await SignInAsync(user, false);
                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
            return View();
        }
 private async Task SignInAsync(BlogUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(UserManager));
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new BlogUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, 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);
                        // SendEmail(user.Email, callbackUrl, "Confirm your account", "Please confirm your account by clicking this link");

                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var info = await Authentication.GetExternalLoginInfoAsync();
            if (info == null)
            {
                return InternalServerError();
            }

            var user = new BlogUser { UserName = model.Email, Email = model.Email };

            var result = await UserManager.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            result = await UserManager.AddLoginAsync(user.Id, info.Login);
            return !result.Succeeded ? GetErrorResult(result) : Ok();
        }
        public async Task<IHttpActionResult> Register(BlogRegisterModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new BlogUser { UserName = model.Username, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded) return GetErrorResult(result);

            var rolesResult = await UserManager.AddToRolesAsync(user.Id, new[] { "Blogger" });
            if (!rolesResult.Succeeded) return GetErrorResult(result);
            
            var blogUser = await AddBlogUser(model);
            return blogUser.Error != null ? GetErrorResult(result) : Ok();
        }
        public async Task<ActionResult> Create(CreateUserViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ViewBag.ErrorMessage = "Something went wrong. Please try again.";
                    return View(GetDefaultCreateUserModel());
                }

                var user = new BlogUser { UserName = model.Username, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var roleResult = await UserManager.AddToRolesAsync(user.Id, new[] { model.SelectedRole });
                    if (!roleResult.Succeeded)
                    {
                        AddErrors(roleResult);
                        return View(GetDefaultCreateUserModel());
                    }

                    if (model.IdentityOnly) return RedirectToAction("Index", "Users");

                    var blogUser = await _userHelper.AddBlogUser(model);
                    if (blogUser.Error == null) return RedirectToAction("Index", "Users");

                    ViewBag.UserCreationError = blogUser.Error.Message;
                    return View(model);
                }
                AddErrors(result);
                return View(GetDefaultCreateUserModel());
            }
            catch (Exception ex)
            {
                _errorSignaler.SignalFromCurrentContext(ex);
                ViewBag.ErrorMessage = ex.Message;
                return View(GetDefaultCreateUserModel());
            }
        }