【发布时间】:2016-01-28 10:05:56
【问题描述】:
我有一个自定义 AuthorizeAttribute:
public class MyAuthAttribute:AuthorizeAttribute {
protected override bool AuthorizeCore(HttpContextBase httpContext) {
return CurrentUser.Roles.Contains(this.Roles);
}
}
现在return Currentuser.Roles 工作得很好。如果返回 false,浏览器会显示 401。
但我想添加其他信息,例如要求的角色。所以我会自己抛出一个异常,而不是 return:
throw new httpException(401,string.Format("User should have been in one of the following roles: {0}",this.Roles);
可以在 AuthorizeAttribute 中抛出 401-Exception 而不是只返回 false 吗?还是有其他(更好的)方法可以将这些信息发送到浏览器?
【问题讨论】:
-
您确定要向恶意用户公开这些附加信息吗?
-
是的。用户应该能够知道缺少哪些组,然后请求管理员授予他访问权限。
标签: c# asp.net-mvc error-handling authorize-attribute custom-authentication