2009-09-28 6 views
1

J'ai créé un simple service WCF REST que j'ai l'intention de consommer depuis une application iPhone. Le service fonctionne bien mais maintenant je voudrais le sécuriser.Sécurisation du service WCF REST pour une utilisation avec l'application iPhone

Dans mon environnement de test (IIS sous Windows 7), j'ai déjà configuré un certificat auto-signé en utilisant makecert.exe.

J'ai également surchargé la méthode validate() afin que je puisse utiliser un mot de passe personnalisé & (puisque l'authentification Windows est hors de question).

Maintenant, je suis bloqué pendant plus de deux jours pour comprendre comment configurer tout pour que ça fonctionne.

Mon objectif est maintenant de pouvoir faire une simple requête GET via le navigateur, quelque chose comme:

https://localhost/testservice/service1.svc/sayHello

Quand cela fonctionnera, je vais continuer à tous les trucs liés à l'iPhone.

Toute aide/exemple sera grandement apprécié!

Voici mon web.config:

<system.serviceModel> 
<services> 
    <service name="IphoneWcf.Service1" behaviorConfiguration="IphoneWcf.Service1Behavior"> 
    <!-- Service Endpoints --> 
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="webBinding" behaviorConfiguration="webBehavior" contract="IphoneWcf.IService1"> 
     <!-- 
      Upon deployment, the following identity element should be removed or replaced to reflect the 
      identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
      automatically. 
     --> 
     <identity> 
     <dns value="localhost" /> 
     </identity> 
    </endpoint> 
    <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> --> 
    <host> 
     <baseAddresses> 
     <add baseAddress="https://localhost/iphonewcf" /> 
     </baseAddresses> 
    </host> 
    </service> 
</services> 
<behaviors> 
    <endpointBehaviors> 
    <behavior name="webBehavior"> 

    </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
    <behavior name="IphoneWcf.Service1Behavior"> 
     <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
     <serviceMetadata httpsGetEnabled="false" /> 
     <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <serviceCredentials> 
     <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName" /> 
     </serviceCredentials> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 
<bindings> 
    <basicHttpBinding> 
    <binding name="webBinding"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Basic" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

Merci à l'avance!

Répondre

0

Vous ne devriez avoir à remplacer aucune méthode. Il suffit de déclarer Digest la sécurité des transports dans votre basicHttpBinding:

<bindings> 
    <basicHttpBinding> 
    <binding name="SecurityByTransport"> 
     <security mode="Transport"> 
     <transport clientCredentialType="Digest" /> 
     </security> 
    </binding> 
    </basicHttpBinding> 
</bindings> 

En vous application iPhone vous permet de gérer le message NSURLConnectionDelegateconnection:didReceiveAuthenticationChallenge comme indiqué dans Handling Authentication Challenges:

-(void)connection:(NSURLConnection *)connection 
     didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 
    if ([challenge previousFailureCount] == 0) { 
     NSURLCredential *newCredential; 
     newCredential=[NSURLCredential credentialWithUser:[self preferencesName] 
               password:[self preferencesPassword] 
               persistence:NSURLCredentialPersistenceNone]; 
     [[challenge sender] useCredential:newCredential 
       forAuthenticationChallenge:challenge]; 
    } else { 
     [[challenge sender] cancelAuthenticationChallenge:challenge]; 
    } 
} 
+1

Presque là .. Je ne peux pas utiliser le condensé, car ma machine de test ne fait partie d'aucun domaine. Donc, pour commencer, j'utilise une authentification de base. Après avoir entré le nom d'utilisateur/mot de passe, j'obtiens une erreur "ressource non trouvée" –

+0

En fait, je ne savais pas que IIS ne peut authentifier les utilisateurs digest que par rapport à un répertoire de domaine. Je suppose que je pensais aux modules http du fournisseur d'authentification ASP.Net, comme dans http://msdn.microsoft.com/en-us/library/bb386455.aspx. Où obtenez-vous l'erreur 'resource not found'? –

+0

Lorsque j'effectue une requête GET dans Internet Explorer pour l'URL suivante: https: //localhost/testservice/service1.svc/sayHello Je reçois un message demandant un nom d'utilisateur et un mot de passe. Après avoir fourni le nom d'utilisateur et mot de passe associé à mon compte Windows - juste là je reçois la page ne peut pas être trouvé erreur –

1

Une autre option serait de créer un autre cert pour l'authentification du client. Je suis sûr que l'iPhone prend en charge les certificats X509. Changez juste votre config pour avoir un type de certificat d'identification de client. Cela fonctionne pour l'authentification de base, mais vous pouvez toujours avoir besoin de l'utilisateur personnalisé/nom pwd sur Basic si vous voulez identifier chaque client de façon unique.

Questions connexes