我正在尝试使用MVC中的ActionFilterAttribute实现身份验证,我正在寻找一些已经完成此类身份验证的示例mvc项目?
答案 0 :(得分:1)
答案 1 :(得分:0)
动作过滤器本身并不多,具体的身份验证将取决于您未详细说明的要求:
/// <summary>
/// Custom authorization attribute for use on controllers and actions.
/// Throws an exception if the user is not authorized.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class ActionPermissionAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
// Override OnAuthorization, not AuthorizeCore as AuthorizeCore will force user login prompt rather than inform the user of the issue.
var controller = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
var action = filterContext.ActionDescriptor.ActionName;
var user = HttpContext.Current.Request.LogonUserIdentity;
// Check user can use the app or the specific controller/action
var authorised = ...
if (!authorised)
throw new UnauthorizedAccessException("You are not authorised to perform this action.");
base.OnAuthorization(filterContext);
}
}