كيفية استخدام eventizeTribute المخصص لوحدة التحكم باستخدام قيمة المعلمة؟

StackOverflow https://stackoverflow.com/questions/2822608

سؤال

أحاول تأمين إجراء وحدة تحكم لمنع المستخدم من الوصول إلى كيان لا يمكنه الوصول إليه. أنا قادر على القيام بذلك مع الكود التالي.

public ActionResult Entity(string entityCode)
{
    if (CurrentUser.VerifyEntityPermission(entityCode))
    {
        //populate viewModel...
        return View(viewModel);
    }
    return RedirectToAction("NoAccessToEntity", "Error");
}

أود أن أكون قادرًا على إضافة سمة إلى إجراء وحدة التحكم نفسها. من أجل التحقق من صحة الوصول إلى الكيان ، أحتاج إلى معرفة القيمة التي تم نقلها إلى وحدة التحكم والكيانات التي يمكن للمستخدم الوصول إليها. هل هذا ممكن؟

[EntityAuthRequired]
public ActionResult Entity(string entityCode)
{
        //populate viewModel...
        return View(viewModel);
}
هل كانت مفيدة؟

المحلول

شيء من هذا القبيل قد يساعدك في طريقك. على الرغم من أنك قد ترغب في إضافة بعض الخصائص الإضافية إلى سمة الخاص بك للسماح لك بتحديد معلمة ContityCode الخاصة بك على كل إجراء ، بدلاً من ترميزها الثابت.

public class EntityAuthRequired : FilterAttribute, IAuthorizationFilter 
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        //Make sure that this is not NULL before assigning value as string...
        var entityCode = filterContext.RouteData.Values["entityCode"] as string;
        // do your logic...         
        if (!allowed)
            filterContext.Result = new HttpUnauthorizedResult();            
    }
}

أيضا ، إذا entityCode ليس في Routedata الخاص بك ، يمكنك استخدامه filterContext.RequestContext.HttpContext.Request للنظر في بيانات البريد.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top