View Full Version : سوال: شرط گذاشتن برای controller
  
morika
جمعه 19 دی 1393, 01:01 صبح
چطور مبشه برای controller یه شرط گداشت که اگه مثلا روی آدس www.example.com باز نشده بود کلا باز نشه؟
helpsos
جمعه 19 دی 1393, 01:10 صبح
باید filter تعریف کنی
morika
دوشنبه 22 دی 1393, 17:40 عصر
خب فیلتر رو تعریف کردیم. چطوری حالا تو این فیلتر بگیم که کنترلر بالا نیاد و بره یه صفحه error نشون بده؟
itsurge
دوشنبه 29 دی 1393, 23:58 عصر
نمونه کد ساخت فیلتر  
publicclassAuthAttribute:ActionFilterAttribute{
    publicRoles _authRoles { get;privateset;}
    [Inject]
    privatereadonlyIAuthorizationService _service;
    publicAuthAttribute(Roles roles)
    {
        _authRoles = roles;
    }
    publicoverridevoidOnActionExecuting(ActionExecutin gContext filterContext)
    {
        if(!filterContext.HttpContext.User.Identity.IsAuth enticated)
        {
            string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath ;
            string redirectUrl =string.Format("?returnUrl={0}", redirectOnSuccess);
            string loginUrl =FormsAuthentication.LoginUrl+ redirectUrl;
            filterContext.HttpContext.Response.Redirect(loginU rl,true);
        }
        else
        {
            bool isAuthorized = _service.Authorize(GetUserSession.Id, _authRoles.ToString());
            if(!isAuthorized)
            {
                // TODO: Make custom "Not Authorized" error page.
                thrownewUnauthorizedAccessException("No access");
            }
        }
    }}
 
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.