0

カスタム AuthorizeAttribute があります。

public class MyAuthAttribute:AuthorizeAttribute {
 protected override bool AuthorizeCore(HttpContextBase httpContext) {
    return CurrentUser.Roles.Contains(this.Roles);
 }
}

これでreturn Currentuser.Roles完全に正常に動作します。false が返された場合、ブラウザは 401 を表示します。

しかし、求められた役割などの追加情報を追加したいと思います。したがって、リターンの代わりに、自分で例外をスローします。

throw new httpException(401,string.Format("User should have been in one of the following roles: {0}",this.Roles);

false を返すだけでなく、AuthorizeAttribute 内で 401-Exception をスローしても問題ありませんか? または、その情報をブラウザーに取得する他の (より良い) 方法はありますか?

4

1 に答える 1