asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc-3 – 在ASP.NET MVC3中的自定义授权属性中使用操作参数大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个控制器,只能在加载特定参数时才请求授权。就像参数ID为8时一样。

我想出了使用这样的自定义验证属性:

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(httpContextBase httpContext)
    {
        if (/* Action's inputparameter ID = 8 */)
        {
        return base.AuthorizeCore(httpContext);
        }
        return true;
    }
}

我的行动看起来像这样(不是很有趣)

[MyAuthorize]
public ActionResult Protected(int id)
{
    /* custom logic for setTing the viewmodel from the id parameter */
    return View(viewmodel);
}

问题是您可以看到我不知道如何在authorize属性中检查该ID参数。
你可以帮我一个解决方案吗?

解决方法

如果id作为请求参数(GET或POST)或路由数据参数传递:
protected override bool AuthorizeCore(httpContextBase httpContext)
{
    // first look at routedata then at request parameter:
    var id = (httpContext.request.requestContext.RouteData.Values["id"] as String) 
             ??
             (httpContext.request["id"] as String);
    if (id == "8")
    {
        return base.AuthorizeCore(httpContext);
    }
    return true;
}

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc-3 – 在ASP.NET MVC3中的自定义授权属性中使用操作参数全部内容,希望文章能够帮你解决asp.net-mvc-3 – 在ASP.NET MVC3中的自定义授权属性中使用操作参数所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。