I can't get data from ViewBag

73 Views Asked by At

I'm doing user role control with Attribute.

Role check page: / forum

I warn if the user is not authorized to view the "forum" page.

I give the warning as follows.

I embed the "filterContext.Result" a "/ Account / NotAuthorizeted" page without changing the link.

Attribute codes are below;

using cms.service.Core;
using cms.service.Services;
using cms.service.Structure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace cms.service.Attributes
{
    public class AccountRoleRequired : ActionFilterAttribute
    {
        public string[] AuthorizationCode { get; set; } = new string[] { };
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            if (!AuthenticationServices.Member.AuthenticateControl())
            {
                HttpCookie cookie = new HttpCookie(Consts.CookieAdmin);
                cookie["Referrer"] = HttpContext.Current.Request.Url.ToString();
                cookie.Expires = DateTime.Now.AddMinutes(5);
                HttpContext.Current.Response.Cookies.Add(cookie);
                filterContext.Result = new RedirectResult(cms.service.Services.SystemServices.GetSysUrl("Account", "Login").Url);
            }
            else
            {
                int NotChecked = AuthorizationCode.Length;
                for (int i = 0; i < AuthorizationCode.Length; i++)
                {
                    if (AuthenticationServices.Member.Get.Roles().Where(w => w.AuthorizationCode == AuthorizationCode[i]).FirstOrDefault() == null)
                    {
                        NotChecked--;
                    }
                }
                if (NotChecked != AuthorizationCode.Length)
                {
                    filterContext.Result = new ViewResult
                    {
                        ViewName = Consts.View_Front_Account_NotAuthorizated
                    };
                    //filterContext.Result = new RedirectResult(cms.service.Services.SystemServices.GetSysUrl("Account", "NotAuthorized").Url);
                }
            }

            string Action = filterContext.RouteData.Values["action"].ToString();
            string Controller = filterContext.RouteData.Values["controller"].ToString();
            filterContext.Controller.ViewData["RouteController"] = Controller;
            filterContext.Controller.ViewData["RouteAction"] = Action;
        }
    }
}

So far, everything is OK.

I'm throwing RouteController and RouteAction information into ViewData.

But I can't get data with ViewBag in View.

ForumController.cs;

using cms.data.Context;
using cms.service.Attributes;
using cms.service.Core;
using cms.service.Services;
using cms.service.Structure;
using Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace cms.site.Controllers
{
    public class ForumsController : FrontController
    {
        // GET: Forums
        [AccountRoleRequired(AuthorizationCode = new string[] { "ForumViewx" })]
        public ActionResult List()
        {
            var Model = ModulesFrontServices.ForumServices.GetForums(null, false);
            return View(Model);

            //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        [AccountRoleRequired(AuthorizationCode = new string[] { "ForumView", "ForumPostCreate" })]
        public ActionResult Add()
        {
            return View();
        }
    }
}

AccountController.cs;

using cms.service.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace cms.site.Controllers
{
    public class AccountController : FrontController
    {
        // GET: Member
        public ActionResult Login()
        {
            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
        public ActionResult NotAuthorized()
        {
            return View();
        }
    }
}

Base - FrontController.cs;

using cms.service.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;

namespace cms.service.Core
{
    [IsSiteContruction]
    [IsLicense]
    public class FrontController : BaseController
    {
        protected override void Initialize(RequestContext requestContext)
        {
            try
            {
                string Action = requestContext.RouteData.Values["action"].ToString();
                string Controller = requestContext.RouteData.Values["controller"].ToString();
                ViewData["RouteController"] = Controller;
                ViewData["RouteAction"] = Action;
            }
            catch (Exception)
            {
            }
            base.Initialize(requestContext);
        }
    }
}
0

There are 0 best solutions below