![](/img/trans.png)
[英]Return IQueryable<T> from my method? Is it the right way to do it?
[英]What do I return on my custom FilterAttribute if a user doesn't have the right authorization?
這是我的過濾器屬性:
public class RoleAttribute : ActionFilterAttribute
{
public string Roles { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.User.Identity.IsAuthenticated)
{
EfUsuarioRepository usuarioRepository = new EfUsuarioRepository();
var loggedInUser = usuarioRepository.BuscarCuentaPorCorreo(filterContext.HttpContext.User.Identity.Name);
string[] userRoles = usuarioRepository.GetRolesForUser(loggedInUser.UsuarioId);
foreach (string definedRole in this.Roles.Split(','))
{
foreach (string role in userRoles)
{
if (definedRole.Equals(role))
return;
}
}
throw new SecurityException("Access not granted!");
}
else
{
throw new SecurityException("Access not granted!");
}
}
}
現在我的問題是重定向到未授權頁面的首選方法是什么? 我是否將自己注入HttpContext並執行重定向? 什么是經證實的模式? 我只想要一個“未經授權”的頁面作為一個全部。
您應該從AuthorizeAttribute
繼承,而不是從ActionFilterAttribute
繼承。
此問題的代碼看起來類似於您嘗試執行的操作,返回類型是true
或false
的布爾值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.