【问题标题】:ASP.NET MVC: Where can I get sample MVC Projects? [closed]ASP.NET MVC:我在哪里可以获得示例 MVC 项目? [关闭]
【发布时间】:2015-07-31 15:06:29
【问题描述】:
我正在尝试在 MVC 中使用 ActionFilterAttribute 实现身份验证,我正在寻找一些已完成此类身份验证的示例 mvc 项目?
【问题讨论】:
标签:
c#
asp.net-mvc
asp.net-mvc-4
c#-4.0
model-view-controller
【解决方案2】:
动作过滤器本身并不多,具体的身份验证将取决于您没有详细说明的要求:
/// <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);
}
}