Logoff (SignOut) user after Delete account in ASP.Net MVC

Last Reply one day ago By bigbear

Posted 14 days ago

I have a action in a controller to delete my user. When I delete the user the user is still signed in. I would like to sign the user out after or before I delete the account. I tried a bunch of ways to do this but have not had any luck but now my idea is to create an instance of accountcontroller and call the default logout method. When I create a new accountcontroller it says that its null in ApplicationSignInManager SignInManager { get { return _signInManager ?? HttpContext.GetOwinContext().Get();

How can I pass my current user into the account controller? Here is some of the code I tried to logout and here is my action method.

[HttpPost, ActionName("DeleteUser")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(string id)
        {
            if (ModelState.IsValid)
            {
                if(id==null)//(model == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                AccountController controller = new AccountController();
                var user = await UserManager.FindByIdAsync(id);//(model.UserId);
                var logins = user.Logins;
                var roles = await UserManager.GetRolesAsync(user.Id);
                if(logins != null)
                {
                    foreach(var login in logins)
                    {
                        await UserManager.RemoveLoginAsync(login.UserId,
                            new UserLoginInfo(
                                login.LoginProvider, login.ProviderKey));
                    }
                }
                // Supposed to only be 1 role but just in case -
                if(roles.Count() > 0)
                {
                    foreach(var r in roles)
                    {
                        var deleteRole = await UserManager.RemoveFromRoleAsync(user.Id, r);
                    }
                }
                controller.LogOff();
                //WebSecurity.Logout() doesnt find.
                //FormsAuthentication.SignOut();
                //HttpContext.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
                //Roles.DeleteCookie();
                //Session.Clear();
                var result = await UserManager.DeleteAsync(user);
                if (result.Succeeded)
                {
                    TempData["Success"] = "User Deleted Successfully";
                    return RedirectToAction("UsersTable", "Users");
                }                
            }
            TempData["Error"] = "User Delete Unsuccessful";
            return RedirectToAction("UserDashboard", "Users");
        }

 


Posted one day ago

I solved with this

 public IAuthenticationManager AuthManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }

and

AuthManager.SignOut();