簡體   English   中英

如果用戶沒有正確的授權,我在自定義FilterAttribute上返回什么?

[英]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繼承。

問題的代碼看起來類似於您嘗試執行的操作,返回類型是truefalse的布爾值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM