2009-02-16 11 views
12

MVC'de varolan Yetkilendirme özniteliğini genişletmek için nasıl özel bir öznitelik oluşturabilirim?asp.net mvc AUTHORIZE özelliğinin eklenmesi

+0

Lütfen tam olarak ne genişletmek istediğinizi daha fazla ayrıntı ekleyin. Şimdilik sadece –

+0

, sadece varsayılan giriş sayfasından ziyade doğru sayfaya yönlendirmek istiyorum. – zsharp

+5

Sorunuzu güncelleyebilirsiniz, böylece herkes neye ihtiyacınız olduğunu bilecektir. –

cevap

17

Sınıfınızı AuthorizeAttribute'dan türetin. Yetkilendirme yöntemini geçersiz kıl. Bir CacheValidationHandler ekleyin ve kurun. Bu özellik uzatmak gerekmez

public void CacheValidationHandler(HttpContext context, 
            object data, 
            ref HttpValidationStatus validationStatus) 
{ 
    validationStatus = OnCacheAuthorization(new HttpContextWrapper(context)); 
} 


public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    if (filterContext == null) 
    { 
     throw new ArgumentNullException("filterContext"); 
    } 

    if (AuthorizeCore(filterContext.HttpContext)) 
    { 
     ... your custom code ... 
     SetCachePolicy(filterContext); 
    } 
    else if (!filterContext.HttpContext.User.Identity.IsAuthenticated) 
    { 
     // auth failed, redirect to login page 
     filterContext.Result = new HttpUnauthorizedResult(); 
    } 
    else 
    { 
     ... handle a different case than not authenticated 
    } 
} 


protected void SetCachePolicy(AuthorizationContext filterContext) 
{ 
    // ** IMPORTANT ** 
    // Since we're performing authorization at the action level, the authorization code runs 
    // after the output caching module. In the worst case this could allow an authorized user 
    // to cause the page to be cached, then an unauthorized user would later be served the 
    // cached page. We work around this by telling proxies not to cache the sensitive page, 
    // then we hook our custom authorization code into the caching mechanism so that we have 
    // the final say on whether a page should be served from the cache. 
    HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache; 
    cachePolicy.SetProxyMaxAge(new TimeSpan(0)); 
    cachePolicy.AddValidationCallback(CacheValidationHandler, null /* data */); 
} 
+0

tamam, ancak son sayfaya doğru olarak nasıl yönlendirebilirim? – zsharp

+0

Bu işi Rol'ler ile nasıl yapabilirim? Şimdi işe yarıyor ama rolleri çalışmıyor gibi görünüyor. Ayrıca AuthorizeCore, kullanıcı Authenticated olduğunda bile false döndürmeye devam eder, bu da SetCachePolicy() 'nın asla çalıştırılmayacağı anlamına gelir. –

+0

@Nick - http://farm-fresh-code.blogspot.com/2011/03/revisiting-custom-authorization-in.html – tvanfosson

3
public class CoolAuthorizeAttribute : AuthorizeAttribute 
{ 
} 
10

, web.config yeterlidir. Lütfen forms Element for authentication hakkında okuyun. Dikkatinizi defaultUrl üzerinde yapın. İhtiyacın olan şey bu. Sadece şimdiki AuthorizeAttribute uzatmak ve bunun yerine OnAuthorization sadece AuthorizeCore geçersiz kılmak ve buna da MyCustomAuthorizationHolds koşul ekleyin geçersiz kılma, bunun üzerine kendi yetki eklemek istiyorsanız

<system.web> 
    <authentication mode="Forms"> 
    <forms defaultUrl="YourUrlGoesHere"/> 
    </authentication> 
</system.web> 
+0

adresinin önbellek işleme özelliklerini iyileştirdiğinden beri blog'um var ama bu dinamik değil. URL değişiyor. – zsharp

+7

Hm, neden bir çözüm vermeden önce tüm istekleri belirtmemelisiniz? –

+1

Noo! Bu tamamen yanlıştır: http://blogs.msdn.com/b/rickandy/archive/2010/08/24/securing-your-mvc-application.aspx – bplus

0

ben önermek.

public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    // This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method. 
    protected override bool AuthorizeCore(HttpContextBase httpContext) 
    { 
     if (base.AuthorizeCore(httpContext) && MyCustomAuthorizationHolds) 
      return true; 

     return false; 
    } 
}