我在asp.net mvc4应用程序中使用了自定义 Membershipprovider 和 roleprovider 类。
这段代码工作正常:
[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");
}
}
但我需要验证Login
操作
public ActionResult Login(string ReturnUrl)
{
//verification and redirection if connected
return View(new User());
}
我需要知道:
答案 0 :(得分:2)
你可以这样检查:
if (User.Identity.IsAuthenticated)
{
// user logged in already
}