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

本文关键字:MVC 项目 NET 在哪里 ASP | 更新日期: 2023-09-27 18:05:58

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

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

尝试MVC示例页面,他们有认证的例子,应该是你所追求的。

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

或者有经典的书呆子晚餐

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

对于动作过滤器本身并没有太多的要求,具体的身份验证将取决于您的需求,您没有详细说明:

/// <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);
    }
}