首页 > 解决方案 > asp.net 5,满足某些条件时在控制器中注销

问题描述

遇到某些情况时,我想在控制器中注销,

我的想法是重定向到“/Identity/Account/Logout”

我试过但没用

return RedirectToAction("Logout", "Account");

普通代码是:

public IActionResult Home_Index()
       {
           var Crm_Account_Data = _context.Crm_Account.Where(x => x.Crm_Email.Equals(User.Identity.Name)).FirstOrDefault();
           if (Crm_Account_Data != null)
           {
              return View();
           }
           else
           {
               // Logout
               // return RedirectToAction("Logout", "Account");
           }
       }

标签: asp.net-mvc

解决方案


I would suggest creating two action methods. Login() and Logout() Your Login action would be something like this:

    [HttpPost]
    public ActionResult Login()
    {
        if (!IsValidUser)
        {
            //redirect to login view
        }

        if (IsValidUser)
        {
            //executables (perhaps passing a model)
        }
        return this.RedirectToAction("Index", "Home");
    }

推荐阅读