【问题标题】:verification of the existence of connected user验证连接用户的存在
【发布时间】:2014-04-18 20:50:20
【问题描述】:

我在我的 asp.net mvc4 应用程序中使用了自定义 Membershipproviderroleprovider 类。

这段代码运行良好:

[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());
                      }

我需要知道:

  1. 有哪些不同的方法可以做到这一点?
  2. 什么是最好的?

【问题讨论】:

  • 我不确定您所说的“验证连接用户的存在”是什么意思。你能详细说明一下吗?
  • 如果我使用管理员帐户登录,我会得到欢迎管理页面
  • 您在寻找User.Identity.IsAuthenticated吗?

标签: c# asp.net .net asp.net-mvc-4 membership-provider


【解决方案1】:

你可以这样检查:

if (User.Identity.IsAuthenticated)
{
// user logged in already
}

【讨论】:

    猜你喜欢
    • 2018-02-17
    • 2017-06-24
    • 2013-12-05
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-01
    • 2020-03-21
    相关资源
    最近更新 更多