ASP.NET MVC:我在哪里可以获得示例MVC项目?

时间:2015-07-31 15:06:30

标签: c# asp.net-mvc asp.net-mvc-4 c#-4.0 model-view-controller

我正在尝试使用MVC中的ActionFilterAttribute实现身份验证,我正在寻找一些已经完成此类身份验证的示例mvc项目?

2 个答案:

答案 0 :(得分:1)

尝试使用MVC示例页面,他们会提供您应该使用的身份验证示例。

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

还有经典的书呆子晚餐

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

答案 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);
    }
}