【发布时间】:2014-04-18 20:50:20
【问题描述】:
我在我的 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());
}
我需要知道:
- 有哪些不同的方法可以做到这一点?
- 什么是最好的?
【问题讨论】:
-
我不确定您所说的“验证连接用户的存在”是什么意思。你能详细说明一下吗?
-
如果我使用管理员帐户登录,我会得到欢迎管理页面
-
您在寻找
User.Identity.IsAuthenticated吗?
标签: c# asp.net .net asp.net-mvc-4 membership-provider