【问题标题】: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


【解决方案1】:

试试 MVC 示例页面,他们提供的身份验证示例应该是您所追求的。

http://www.asp.net/aspnet/samples/aspnet-mvc

或者有经典的书呆子晚餐

http://nerddinnerbook.s3.amazonaws.com/Intro.htm

【讨论】:

    【解决方案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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-07
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多