Question

i have used custom Membershipprovider and roleprovider class in my asp.net mvc4 application.

this code works fine :

[OutputCache(Duration =0, NoStore= true)]
    public class HomeController : Controller
    {
      public ActionResult Login(string ReturnUrl)
         { 
            return View(new User());
                  }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(User u, string ReturnUrl) {
            if (Membership.ValidateUser(u.login, u.password))
            {
                FormsAuthentication.SetAuthCookie(u.login, false);
               if (Roles.GetRolesForUser(u.login).Contains("user")) return RedirectToAction("Index");
                else return RedirectToAction("Common");
            }
            else {
                return View(u);
            }
        }

        [Authorize(Roles = "user")]
        public ActionResult Index()
        {
            return View();
        }
         [Authorize(Roles="admin")]
        public ActionResult Common()
        {
            return View();
        }

         public ActionResult SignOut()
         {
             FormsAuthentication.SignOut();
              return RedirectToAction("Login");
         }
    }

But i need to verify if the existence of a connected user in the Login action

 public ActionResult Login(string ReturnUrl)
             { 
              //verification and redirection if connected
                return View(new User());
                      }

I need to know:

  1. What are the different manners to do that?
  2. What is the best one?
Was it helpful?

Solution

you can check this way:

if (User.Identity.IsAuthenticated)
{
// user logged in already
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top