Привет Я пытаюсь использовать настраиваемый атрибут authorize, но base.AuthorizeCore всегда возвращает false. Я понятия не имею, где я ошибся. Не могли бы вы рассказать мне, где проблема, пожалуйста. Мой AuthorizeAttribute:base.AuthorizeCore (httpContext) allways 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;
}
}
Мой метод LogIn:
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();
}
спасибо, роль, взятая с Db, была «Пользователь», а роль в атрибуте была «Пользователь», – Zet