2012-04-13 6 views
1

2-legged OAuth를 전제로 웹 서비스를 구축하고 있습니다.MVC3에서 HMAC 인증

각 인증 된 요청에는 포함 된 HMAC가 있습니다.

public ActionResult userInfoExample(string HMAC, string username) 
    { 
     MyMembership.checkHMAC(HMAC); 
     //get user 
     return View(); 
    } 

을하지만 HMAC는 모든 행동의 매개 변수에 포함 할 필요가 있기 때문에 즉, 상당히 불쾌한 :

나는이 다음과 같이 할 수 있습니다 알고있다. 그것의 약하게 입력하고 쓰레기.

[AuthorizeHMAC] 
    public ActionResult userInfoExample(string username) 
    { 
     //get user 
     return View(); 
    } 

I found this을, 그리고 내가 그럼 I found this, 사용자 정의 모달 바인더를 보라 언급을 읽은 후 나는 그 일을 할 수 있는지 확실하지 오전 :

나는 이런 식으로 뭔가를하고 싶었다.

목표는 URL 매개 변수 즉, http : // www.website.com/foo/bar?username=xxx & hmac = xxxxxxxxx에 배치 된 HMAC를 사용하여 인증 (/ 승인)하는 것입니다.

누구든지 내가 읽을 수있는 참고 문헌이나 직접적인 해결책이 있는지 알고 싶습니다.
는 또한 API 보안의 내 기본적인 이해에 대한 비판에 오신 것을 환영하고, 또는 내가 일을하고 어떻게 내가 비슷한 할 http://mvcsecurity.codeplex.com/

에서

답변

1

체크 아웃 내 코드의이 지역에 비교적 새로운 오전 페이지의 매개 변수를 확인합니다 (HMAC는 아닙니다). 뷰 Im (또는보기로 전달)에서 그것을 생성 할 것이므로 동일한 방식으로 내 속성에서 확인하는 것과 같은 방법으로이를 확인할 수 있습니다. 에서

:

 

     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      //The hidden form field that contains our hash - for ex. CustomerId is rendered as a hidden input id="_CustomerIdToken" 
      string encryptedPropertyName = string.Format("_{0}Token", _propertyName); 

      //grab the token 
      string hashToken = filterContext.HttpContext.Request.Form[encryptedPropertyName]; 

      //The encrypted form data MUST be there. We do not allow empty strings otherwise this could give 
      //an attack vector in our filter as a means to bypass checks by simply passing in an empty validation token. 
      if (string.IsNullOrEmpty(hashToken)) 
      { 
       throw new MissingFieldException(string.Format("The hidden form field named value {0} was missing. This is created by the Html.AntiModelInjection methods. Ensure the name used on your [ValidateAntiModelInjectionAttribute(\"!HERE!\")] matches the field name used in Html.AntiModelInjection method. If this attribute is used on a controller method that is meant for HttpGet, then the form value would not yet exist. This attribute is meant to be used on controller methods accessed via HttpPost.", encryptedPropertyName)); 
      } 


      //Get the plain text value 
      string formValue = filterContext.HttpContext.Request.Form[_propertyName]; 

      //Plain text must be available to compare. 
      if (string.IsNullOrEmpty(formValue)) 
      { 
       throw new MissingFieldException(string.Format("The form value {0} was missing. If this attribute is used on a controller method that is meant for HttpGet, then the form value would not yet exist. This attribute is meant to be used on controller methods accessed via HttpPost.", _propertyName)); 
      } 


      //We cannot encrypt the form value and compare to the previously encrypted form token. 
      //Each time you Encrypt() with the MachineKey class even using the same plain text, the end result is difference. 
      byte[] plainTextBytes = MachineKey.Decode(hashToken, MachineKeyProtection.Encryption); 

      string plainText = Encoding.Unicode.GetString(plainTextBytes); 

      //And compare 
      if (string.Compare(plainText, formValue , false, CultureInfo.InvariantCulture) != 0) 
      { 
       throw new HttpAntiModelInjectionException(string.Format("Failed security validation for {0}. It is possible the data was tampered with as the original value used to create the form field does not match the current property value for this field. Ensure if this is a web farm, the machine keys are the same.",_propertyName)); 
      } 


      filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " + 
       filterContext.ActionDescriptor.ActionName); 

      base.OnActionExecuting(filterContext); 
     } 
 
+0

그 사이트는 오히려 혼란이다. 관련 코드의 스 니펫을 여기에 게시 할 수 있습니까? (또한 태블릿에있어 볼 필요가 없습니다.) – MrJD

+0

출처 : http://mvcsecurity.codeplex.com/SourceControl/changeset/view/13151#149140 관련 섹션은 귀하의 시간에 대해 –

+0

+1의 양식 값. 나는 그것이 오늘 작동하는지보기 위해 오늘 그것을 시험해 볼 것이다. – MrJD