0
は私のフィルタ属性です:ユーザが適切な権限を持っていない場合、私のカスタムFilterAttributeには何が返されますか?ここ
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に挿入し、リダイレクトを実行しますか?これに対して実績のあるパターンは何ですか?私はただ「Unauthorized」ページをキャッチオールとして望んでいます。
これは正しい方法です。 –
ありがとう、私は、AuthorizeAttributeを使用するように切り替えました。もう一つの最後のビットは、 'filterContext.Result = new RedirectResult("/");'を使用するかどうかにかかわらず、今度はAccount/Logonにリダイレクトされます。どうして? –
ほとんどの場合、web.configに設定されている可能性があります。私はそこにある値が、属性に設定されているフィルタコンテキストの結果をオーバーライドしていると確信しています。 – Lester