本文介绍了是否有可能在一个动作禁止在MVC 5控制器的认证过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [的authenticateUser]
公共类HomeController的:控制器
{
    //
    // 回家/
    公众的ActionResult指数()
    {
        返回查看();
    }    [使用AllowAnonymous]
    公众的ActionResult列表()
    {
        返回查看();
    }
}

如何为命名为列表操作除去身份验证?请指教....

我的自定义过滤器编码如下..我继承了FilterAttribute电话也是如此。
请有关建议

 公共类AuthenticateUserAttribute:FilterAttribute,IAuthenticationFilter
{
    公共无效OnAuthentication(AuthenticationContext上下文)
    {
        如果(this.IsAnonymousAction(上下文))
        {        }        如果(用户==用户)
        {
            // 没做什么
        }
        其他
        {
            context.Result =新HttpUnauthorizedResult(); //标志未经授权
        }
    }    公共无效OnAuthenticationChallenge(AuthenticationChallengeContext上下文)
    {
        如果(context.Result == NULL || context.Result是HttpUnauthorizedResult)
        {
            context.Result =新RedirectToRouteResult(默认,
                新System.Web.Routing.RouteValueDictionary {
                    {控制器,家},
                    {行动,列表},
                    {RETURNURL,context.HttpContext.Request.RawUrl}
                });
        }
    }
}

下面code生成错误消息:错误1的最佳重载方法匹配'MVC5Features.Filters.AuthenticateUserAttribute.IsAnonymousAction(System.Web.Mvc.AuthorizationContext)'有一些无效参数C:\\用户\\ kirupananthan.g \\文档\\的Visual Studio 2013 \\项目\\ MVC5Features \\ MVC5Features \\ \\过滤器16 AuthenticateUserAttribute.cs 17 MVC5Features
错误2参数1:无法从System.Web.Mvc.Filters.AuthenticationContext'到'System.Web.Mvc.AuthorizationContext'C转换:\\用户\\ kirupananthan.g \\文档\\的Visual Studio 2013 \\项目\\ MVC5Features \\ MVC5Features \\过滤器\\ AuthenticateUserAttribute.cs 16 40 MVC5Features

 如果(this.IsAnonymousAction(上下文))


解决方案

既然是你的自定义过滤器,你可以扩展它来处理使用AllowAnonymous (如果你不这样做要使用使用AllowAnonymous,同比可以创建自己的FE NoAuthentication):

 公共类的authenticateUser:IAuthenticationFilter
{    公共无效OnAuthentication(AuthenticationContext filterContext)
    {
        如果(this.IsAnonymousAction(filterContext))
        {
            返回;
        }        //一些code
    }    私人布尔IsAnonymousAction(AuthenticationContext filterContext)
    {
        返回filterContext.ActionDescriptor
                             .GetCustomAttributes(继承:真)
                             .OfType< AllowAnonymousAttribute>()
                                             //或任何ATTR。你要
                             。任何();
    }
}
[AuthenticateUser]
public class HomeController : Controller
{
    //
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    [AllowAnonymous]
    public ActionResult List()
    {
        return View();
    }
}

How to remove authentication for action named as List? Please advise....

My Custom Filter coding as follow.. i have inherited the FilterAttribute call as well. Please advise regarding

public class AuthenticateUserAttribute: FilterAttribute, IAuthenticationFilter
{
    public void OnAuthentication(AuthenticationContext context)
    {
        if (this.IsAnonymousAction(context))
        {

        }

        if (user == "user")
        {
            // do nothing
        }
        else
        {
            context.Result = new HttpUnauthorizedResult(); // mark unauthorized
        }
    }

    public void OnAuthenticationChallenge(AuthenticationChallengeContext context)
    {
        if (context.Result == null || context.Result is HttpUnauthorizedResult)
        {
            context.Result = new RedirectToRouteResult("Default",
                new System.Web.Routing.RouteValueDictionary{
                    {"controller", "Home"},
                    {"action", "List"},
                    {"returnUrl", context.HttpContext.Request.RawUrl}
                });
        }
    }
}

The below code generate the error message : Error 1 The best overloaded method match for 'MVC5Features.Filters.AuthenticateUserAttribute.IsAnonymousAction(System.Web.Mvc.AuthorizationContext)' has some invalid arguments c:\users\kirupananthan.g\documents\visual studio 2013\Projects\MVC5Features\MVC5Features\Filters\AuthenticateUserAttribute.cs 16 17 MVC5FeaturesError 2 Argument 1: cannot convert from 'System.Web.Mvc.Filters.AuthenticationContext' to 'System.Web.Mvc.AuthorizationContext' c:\users\kirupananthan.g\documents\visual studio 2013\Projects\MVC5Features\MVC5Features\Filters\AuthenticateUserAttribute.cs 16 40 MVC5Features

if (this.IsAnonymousAction(context))
解决方案

Since it is your custom filter, you can extend it to handle AllowAnonymous (if you don't want to use AllowAnonymous, yoy can create own f.e. NoAuthentication):

public class AuthenticateUser : IAuthenticationFilter
{

    public void OnAuthentication(AuthenticationContext filterContext)
    { 
        if (this.IsAnonymousAction(filterContext))
        {
            return;
        }

        // some code
    }

    private bool IsAnonymousAction(AuthenticationContext filterContext)
    {
        return  filterContext.ActionDescriptor
                             .GetCustomAttributes(inherit: true)
                             .OfType<AllowAnonymousAttribute>() 
                                             //or any attr. you want
                             .Any();
    }
}

这篇关于是否有可能在一个动作禁止在MVC 5控制器的认证过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 16:58