C# AuthorizeCore(httpContext)总是错误的-如何找到原因

C# AuthorizeCore(httpContext)总是错误的-如何找到原因,c#,asp.net-mvc,authorization,C#,Asp.net Mvc,Authorization,您好,我正在尝试使用自定义授权属性,但base.AuthorizeCore始终返回false。我不知道我哪里做错了。你能告诉我哪里出了问题吗。我的个性: public class AuthorizeUserAttribute : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.Autho

您好,我正在尝试使用自定义授权属性,但base.AuthorizeCore始终返回false。我不知道我哪里做错了。你能告诉我哪里出了问题吗。我的个性:

public class AuthorizeUserAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var isAuthorized = base.AuthorizeCore(httpContext);
        if (!isAuthorized)
        {
            return false;
        }
        string roles = string.Join("", httpContext.Session["UserRole"]);
       // string roles = string.Join("", HttpContext.Current.Session["UserRole"]);
        if (Roles.Contains(roles))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
我的登录方式:

 public ActionResult LogIn()
 {
        var model = new UserModel();
        return View(model);
 }


 [HttpPost]
 public ActionResult LogIn(UserModel model)
 {

        if (!ModelState.IsValid)
        {
            return View("LogIn", model);
        }
        else
            {
            var usermodelDB = _UserAccountService.GetUser(model.Password);
            if (model.userName == usermodelDB.userName && model.Password==usermodelDB.Password)
            {


                model.userRole = usermodelDB.userRole;
                FormsAuthentication.SetAuthCookie(model.userRole, true);
                System.Web.HttpContext.Current.Session["UserRole"] = usermodelDB.userRole;
                var ia =System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
            }
            return View("LogIn", model);
        }
 }
以及访问受限的方法:

[AuthorizeUser(Roles="User")]
public ActionResult Index(int page=0)
{
    return View();
}

用户很可能不在该角色中
AuthorizeCore
查看用户的身份并测试用户所处的角色。因此,如果用户获得授权,则返回true。()

对我来说,我删除了网站的cookies,然后它就工作了。似乎两个应用程序使用相同的db表存储用户。更改cookie名称有助于解决错误:

谢谢,从Db获取的角色是“User”,属性中的角色是“User”