2011-05-03 38 views
5

Birkaç günlüğüne bunun için şaka yapıyorum.WCF .net 4.0 HTTP Temel Kimlik Doğrulaması

Sadece Basit HTTP Kimlik Doğrulamalarını kullanarak RESTful hizmetime geçirilen bir Kullanıcı Adı/Parola almayı deniyorum. Her şey garip çalışır!

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace ParkingPandaREST 
{ 
    public class CustomUserNameValidator : System.IdentityModel.Selectors.UserNamePasswordValidator 
    { 
     // This method validates users. It allows in two users, test1 and test2 
     // with passwords 1tset and 2tset respectively. 
     // This code is for illustration purposes only and 
     // must not be used in a production environment because it is not secure.  
     public override void Validate(string userName, string password) 
     { 
      if (null == userName || null == password) 
      { 
       throw new ArgumentNullException(); 
      } 

      if (!(userName == "test1" && password == "1tset") && !(userName == "test2" && password == "2tset")) 
      { 
       // This throws an informative fault to the client. 
       throw new System.ServiceModel.FaultException("Unknown Username or Incorrect Password"); 
       // When you do not want to throw an infomative fault to the client, 
       // throw the following exception. 
       // throw new SecurityTokenException("Unknown Username or Incorrect Password"); 
      } 
     } 
    } 
} 

ben sadece benim yerel makinede hizmetini çalıştıran ve am:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    </system.webServer> 

    <system.serviceModel> 

    <bindings> 
     <wsHttpBinding> 
     <binding name=""> 
      <security mode="Message"> 
      <message clientCredentialType="UserName" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 

    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true"/> 
     </webHttpEndpoint> 
    </standardEndpoints> 

    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceCredentials> 
      <userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="ParkingPandaREST.CustomUserNameValidator, ParkingPandaREST" /> 
      </serviceCredentials>  
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 


    </system.serviceModel> 

</configuration> 

Sonra aşağıdaki olan bir CustomUserNameValidator sınıfını yarattı: Burada

benim web.config neye benzediği CustomUserNameValidator sınıfında bir break noktasına vurmaya çalışıyor ancak asla oraya ulaşmıyor. Bu yüzden şu anda SSL kullanmıyorum, sadece kullanıcı adı/Şifre giriliyor.

cevap

4

Buraya geldiğim cevap. Biraz düzeltilmesi gerekebilir, ancak işi yapmak için ihtiyacınız olan çekirdek.

Not: Ayrıca burada bazı diğer insanlara yardım DecodeBase64String fonksiyonu

public static string DecodeBase64String(string encodedData) 
    { 
     byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData); 
     string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes); 
     return returnValue; 
    } 

Hope ....... IIS

  // Get the Header Authorization Value 
      string headerValue = operationContext.IncomingRequest.Headers[HttpRequestHeader.Authorization]; 
      headerValue = headerValue.Replace("Basic ", ""); 
      headerValue = DecodeBase64String(headerValue); 

      // Get Username and Password 
      string userName = headerValue.Substring(0, headerValue.IndexOf(":")); 
      string password = headerValue.Substring(headerValue.IndexOf(":") + 1, headerValue.Length - userName.Length - 1); 

      // Do Custom Authorization here with the userName and password 

yılında

temel kimlik doğrulamayı kapatın kim aynı sorunu yaşıyorlardı - şerefe!

+3

'u çalıştırmama özel validator'umla aynı sorunu yaşıyorum Enconding.UTF8.GetString (encodedData) kullanılmasını öneririm –

+0

Bu kodu nereye koydunuz? – jao

1

WCF 4'teki HTTP aktarımı için varsayılan bağlanma basicHttpBinding'dir. WsHttpBinding öğenizi bir basicHttpBinding öğesine dönüştürür ve özel doğrulama için yapılandırırsanız, kodunuz çalıştırılmalıdır.

+0

öğesini olarak değiştirmeyi denedim ve yine de kırılma noktasına ulaşamadım. Başka hiçbir şey eksik miyim? – Adam

+0

İstemci yapılandırmanızın ve servis yapılandırmanızın senkronize olmaması olabilir. Ayrıca, HTTP temel kimlik doğrulamasına izin vermek için IIS'yi yapılandırmanız gerekir. IIS yapılandırması, sürümüne göre değişecektir, ancak burada IIS 7 yapılandırması: http://technet.microsoft.com/en-us/library/cc772009(WS.10).aspx –

+0

Bu, yerel geliştirici makinede çalışmıyor. VS2010 - Sadece F5'i vuruyor ve hata ayıklama modunda mı yaşıyorsun? – Adam

0

Temel kimlik doğrulaması bir HTTP protokolüdür. Böylece ileti güvenliği değil, nakliye güvenliği. Eğer bunu doğru olsa bile

<security mode="Transport"> 
    <transport clientCredentialType="Basic" /> 
</security> 

Fakat, özel kod muhtemelen

denilen olmayacak: Sizin yapılandırma bu pasajını içermelidir. Bu sorunla ilgili daha fazla bilgiyi bu blog adresinde bulabilirsiniz.

+0

Hizmetlerim için temel kimlik doğrulamayı kullanmak için kullanıyorum: http://altairiswebsecurity.codeplex.com/ Hizmetlerimi WCF yerine bir MVC denetleyicisi olarak barındırıyorum. –

+0

Yepyeni bir WCF sürümü ile Temel HTTP Doğrulaması'nı düzgün bir şekilde yapamazsınız ... vay bu korkunç! – Adam

+0

@Codo Bağlantınızı takip eden blog :(im de – Dendei

İlgili konular