2012-02-14 6 views
0

WCF Rest webservice를 인증 할 수 있기를 원하지만 실제로 어떻게해야하는지 잘 모르겠습니다. 그것은 더 이상 존재하지 않는 것 같은 WebServiceHost2와 같은 .net 3.5 WCF (WebServiceHost2와 같은)에 관련된 다른 많은 질문처럼 보입니다.WCF REST 인증 동작

사용자 지정 사용자 이름과 암호를 사용하여 WCF 서비스에서 메시지 기반 인증을 수행하려고합니다. 나는이 Web.config를 기반으로 동작 설정이 야 어차피 나머지를 사용하고 같은

<behaviors> 
    <serviceBehaviors> 
    <behavior name="PasswordValidator"> 
     <serviceCredentials> 
     <userNameAuthentication userNamePasswordValidationMode="Custom" 
           customUserNamePasswordValidatorType="MyNamespace.PasswordValidator, MyNamespace"/> 
     </serviceCredentials> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

을하지만 : 내가이 말할 수있는 것은 일반 WCF에서 다음을 수행 할 수 있습니다에서. 나는 어떻게 든 내 봉사에서 이것을 할 필요가있다.

RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(HelloService))); 

누구나이 작업을 수행하는 방법이나 Rest 및 WCF 4.0 메시지 기반 보안에 대한 유용한 자습서가 있습니까?

+1

메시지 기반의 보안을 확보하기 위해 내 계약 방법이 속성을 추가 할 수 있습니다 SOAP는 WS-Security를 ​​중심으로 구성됩니다. 자신의 자격 증명 저장소에 대한 인증을 찾고 있습니까? –

+1

다음 링크는 도움이 될 수 있습니다. http://stackoverflow.com/questions/6021612/wcf-restful-web-services-and-custom-authentication – Rajesh

+0

http://www.leastprivilege.com/TokenBasedAuthenticationForWCFHTTPRESTServicesAuthentication.aspx – Rajesh

답변

0

내가 해결 한 방법은 HTTP 헤더 컬렉션에 추가 한 두 개의 사용자 정의 필드를 보는 사용자 지정 권한 부여 특성을 구현하는 것이 었습니다.

이것은 꽤 잘 작동하는 것 같습니다.

public class UserAndPasswordAuthenticationAttribute : Attribute, IOperationBehavior, IParameterInspector 
    { 
     public void ApplyDispatchBehavior(
      OperationDescription operationDescription, 
      DispatchOperation dispatchOperation) 
     { 
      dispatchOperation.ParameterInspectors.Add(this); 
     } 

     public void AfterCall(string operationName, object[] outputs, 
           object returnValue, object correlationState) 
     { 
     } 

     public object BeforeCall(string operationName, object[] inputs) 
     { 
      string username = WebOperationContext.Current 
            .IncomingRequest.Headers["username"]; 
      string password = WebOperationContext.Current 
            .IncomingRequest.Headers["password"]; 


      if (username != "bob" || password!= "123") 
      { 
       WebOperationContext.Current.OutgoingResponse.StatusCode = 
        HttpStatusCode.Unauthorized; 
       throw new UnauthorizedAccessException(""); 
      } 

      return null; 
     } 

     public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters) 
     { 
     } 

     public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation) 
     { 
     } 

     public void Validate(OperationDescription operationDescription) 
     { 
     } 
    } 

그때 나는 단지 그들에게