本文介绍了base.AuthorizeCore(HttpContext的)是假的永诺 - 如何找到原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我试图用一个自定义的授权属性,但base.AuthorizeCore传真返回f​​alse。我不知道,我已经做了错误。你能告诉我问题出在哪里吧。我AuthorizeAttribute:

 公共类AuthorizeUserAttribute:AuthorizeAttribute
{
    保护覆盖布尔AuthorizeCore(HttpContextBase的HttpContext)
    {
        VAR isAuthorized = base.AuthorizeCore(HttpContext的);
        如果(!isAuthorized)
        {
            返回false;
        }
        串角色=的string.join(,httpContext.Session [的UserRole]);
       //串角色=的string.join(,HttpContext.Current.Session [的UserRole]);
        如果(Roles.Contains(角色))
        {
            返回true;
        }
        其他
        {
            返回false;
        }
    }

我的登录方式:

 公众的ActionResult登录()
 {
        VAR模型=新的usermodel();
        返回查看(模型);
 }
 [HttpPost]
 公众的ActionResult登录(的usermodel模型)
 {        如果(!ModelState.IsValid)
        {
            返回视图(登录,模型);
        }
        其他
            {
            VAR usermodelDB = _UserAccountService.GetUser(model.Password);
            如果(model.userName == usermodelDB.userName&放大器;&安培; model.Password == usermodelDB.Password)
            {
                model.userRole = usermodelDB.userRole;
                FormsAuthentication.SetAuthCookie(model.userRole,真);
                System.Web.HttpContext.Current.Session [的UserRole] = usermodelDB.userRole;
                VAR IA = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
            }
            返回视图(登录,模型);
        }
 }

和有限的访问方式:

  [AuthorizeUser(角色=用户)]
公众的ActionResult指数(INT页= 0)
{
    返回查看();
}


解决方案

最有可能的用户不在该角色。 AuthorizeCore 看用户的身份和测试,角色的用户。所以,如果用户被授权返回true。 ( + )

Hi I'm trying to use a custom authorize attribute but base.AuthorizeCore allways return false. I have no idea where I've done mistake. Could you tell me where is the problem please. My AuthorizeAttribute:

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;
        }
    }

My LogIn method:

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

and the method with limited access:

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

Most probably the user is not in that role. AuthorizeCore look at the user's identity and test which roles the user is in. So it returns true if the user is authorized. (+)

这篇关于base.AuthorizeCore(HttpContext的)是假的永诺 - 如何找到原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:08