如何在方法调用的属性指定 [英] How to specify attribute on call of method

查看:166
本文介绍了如何在方法调用的属性指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作方法。
它是一种具有2属性

I am having one action method. Which is having 2 attribute

[Authorization]
[OutputCache]
ActionResult LoadImage()

我从两个方法调用的LoadImage行动
说1:指数2:创建

I am calling LoadImage action from two method say 1: Index 2: Create

当我从指数看涨的LoadImage行动,我想的LoadImage的两个属性来执行。
当我打电话从创建的LoadImage行动,我想只有授权属性是执行。
我不希望使用的VaryByParam

When i call LoadImage action from Index, I want both attribute of LoadImage to execute. When i call LoadImage action from Create, I want only Authorization attribute to be execute. I don't want to use VaryByParam.

推荐答案

请看看我刚才的答复,看看是否满足您的要求。如果你真的有实现你在你的问题说,这里是如何...

Please see my earlier answer and see if that satisfy your requirement. If you really have to achieve what you stated in your question, here is how...

定义一个定制的授权属性。检查Request.Params未来值来做出是否应用属性或跳过类似于您实现通过使用AllowAnonymous 属性什么的授权决定。

Define a custom Authorization attribute. Check a value coming in Request.Params to make a decision about whether to apply the attribute or skip the authorization similar to what you achieve through AllowAnonymous attribute.

举例code(将需要一些变化,按您的需要):

Example code (will require some changes as per your need):

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class ProspectProfileAuthorizationAttribute : AuthorizeAttribute
{
   /// <summary>
   /// Special authorization check based on whether request contain valid data or not.
   /// </summary>
   /// <param name="filterContext"></param>
   public override void OnAuthorization(AuthorizationContext filterContext)
   {
       Guard.ArgumentNotNull(filterContext, "filterContext");
       Guard.ArgumentNotNull(filterContext.Controller, "filterContext.Controller");

       bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(
           typeof(CustomAllowAnonymous), inherit: true)
                                || filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
                                    typeof(CustomAllowAnonymous), inherit: true);

       if (skipAuthorization)
       {
           return;
       }

       var request = filterContext.RequestContext.HttpContext.Request;

       NameValueCollection parameterCollection = ReadQueryStringData(filterContext, request);

       if (parameterCollection.Count < 3)
       {
           throw new InvalidOperationException("Request with invalid number of parameter");
       }

       // Check 1: Is request authenticated i.e. coming from browser by a logged in user
       // No further check required.
       if (request.IsAuthenticated)
       {
           return;
       }

       // Check 2: Request is coming from an external source, is it valid i.e. does it contains
       // valid download code.
       if (string.IsNullOrEmpty(downloadCode))
       {
           throw new InvalidOperationException(Constants.Invalid_Download_Code);
       }

       if (!userType.Equals(Constants.SystemIntegrationUserName))
       {
           var exportReportService = DependencyResolver.Current.GetService<IExportReportService>();

           if (exportReportService != null)
           {
               if (!exportReportService.VerifyDownloadCode(downloadCode))
               {
                   // Invalid partner key
                   throw new InvalidOperationException(Constants.Invalid_Download_Code);
               }
           }
       }
   }

   private static NameValueCollection ReadQueryStringData(AuthorizationContext filterContext, HttpRequestBase request)
   {
       // Obtain query string parameter from request
       //original
       //var encryptedData = request.Params["data"];

       // Applying the replace for space with + symb
       var encryptedData = request.Params["data"].Replace(" ","+");

       var decryptedData = EncryptionHelper.DecryptString(encryptedData);

       // Validate the parameter
       var dict = HttpUtility.ParseQueryString(decryptedData);

       return dict;

   }
}

这篇关于如何在方法调用的属性指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆