0

사용자 정의 AuthorizAttribute를 작성하고 AuthorizeCore를 재정의하려합니다. 이 어디에서나 올바르게 작동하지만 특정 역할 없이는 URL을 액세스 할 수 없으므로, 그것은 내가 거기에 갈 수있게 해준다. 내가 "http://localhost:8758/Classified/Attributes"URL을 누르는 것처럼 그것은 관리 역할이 필요하지만 내 코드는 관리 역할없이 액세스 할 수 있습니다. 뭐 잘못 됐나요? 여기 내 코드입니다.사용자 지정 권한 특성에서 AuthorizeCore를 재정의하지만 URL에서 직접 액세스 할 수 있도록 허용합니다.

using System; 
using System.Web; 
using System.Web.Mvc; 
using Classified.Web.Services; 


namespace Classified.Web 
{ 
    public class CustomAuthorizeAttribute : AuthorizeAttribute 
    { 
     public IFormsAuthenticationService AuthenticationService { get; set; } 

     public string RequiredRole; 

     protected override bool AuthorizeCore(HttpContextBase httpContext) 
     { 
      if (httpContext == null) throw new ArgumentNullException("httpContext"); 
      AuthenticationService = new FormsAuthenticationService(new HttpContextWrapper(HttpContext.Current)); 

      var user = AuthenticationService.GetAuthenticatedUser(); 

      if (user == null) 
       return false; 

      foreach (var i in user.Roles) 
      { 
       if (i.RoleName == RequiredRole) 
       { 
        return true; 
       } 
      } 

      return false; 

     } 
    } 
+0

허 ............ 나는 내 스스로가 답을 가지고 :) ... –

답변

0

나는 내 자신에 의해 솔루션을 가지고는 ...

난 그냥 컨트롤러 전에 허가 신청 깜빡 약간의 실수가 있었다.
그런 것.

[Authorize] public class AdminController : Controller { . . .