Reputation: 9081
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:
Upvotes: 0
Views: 42
Reputation: 62488
you can check this way:
if (User.Identity.IsAuthenticated)
{
// user logged in already
}
Upvotes: 2