ASP.NET MVC 4身份验证不起作用

时间:2014-01-14 02:32:39

标签: asp.net-mvc asp.net-mvc-4 authentication

我正在使用ASP.NET MVC4开发一个网站。我有2种不同类型的用户。 “UserA”和“UserB”。

我也希望他们每个人都能访问他们的个人网页,我想在他们登录时添加重定向。

因此,在accountController.cs的登录功能中,我修改了登录功能,如下所示

        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
               if (User.IsInRole("RoleA")){
                    return Redirect("/Home/RoleA")
}
               else if (User.IsInRole("RoleB"))
               {
                   return Redirect("/Home/RoleB");
               }

                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

然而,它输入if,并且表示用户未经过身份验证。因此,

   if (User.IsInRole("RoleA")){

是错误的,尽管用户是“RoleA”。

为什么我的用户未经过身份验证,并且未在其个人网站上重定向? 如果我评论我的代码,那么身份验证工作完美,用户被重定向到索引。

编辑:

我不知道他们是否做同样的工作,但我用了

"Roles.GetRolesForUser("sergey").Contains("Developer")"

我解决了我的问题。我希望没有任何安全或操作漏洞

1 个答案:

答案 0 :(得分:0)

您可能需要考虑阅读http://msdn.microsoft.com/en-us/library/twk5762b.aspx。表单身份验证票证为浏览器发出的“下一个”请求提供表单身份验证信息。

这是另一个post,详细说明了您遇到的特定问题。

相关问题