2013-07-29 91 views
0

我需要使用用戶名令牌來驗證Primavera P6 Web服務。我在VS2010中創建控制檯應用程序,並增加了服務引用:在這一刻如何在C#中使用用戶名令牌來驗證Primavera P6 Web服務?

http://localhost:8206/p6ws/services/ExportService?wsdl 

所以我有一個代理類,我可以寫這樣的事:

var exportService = new ExportPortTypeClient(); 
var project = new ExportProject { ProjectObjectId = 1000 }; 
exportService.ExportProject(project); 

雖然我試圖調用ExportProject()由於身份驗證失敗,我得到一個異常。

有沒有人有使用用戶名令牌進行身份驗證的示例代碼?

+0

您是否找到解決問題的方法?我很難找到正確的安全綁定。請告訴我。 – Rajiv

回答

2

確保您有這樣的習俗結合創建客戶端:

var securityElement = SecurityBindingElement.CreateUserNameOverTransportBindingElement();     
securityElement.AllowInsecureTransport = true; //in case you don't use SSL 
securityElement.EnableUnsecuredResponse = true; //in case you don't use SSL 
var encodingElement = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8); 
var transportElement = new HttpTransportBindingElement(); 
var binding = new CustomBinding(securityElement, encodingElement, transportElement); 

EndpointAddress endpointAddress = new EndpointAddress("<your endpoint to service goes here>"); 

var exportService = new ExportPortTypeClient(binding, endpointAddress); 
var project = new ExportProject { ProjectObjectId = 1000 }; 
exportService.ExportProject(project); 
0

我終於跨過一個解決方案來使用與P6 WebServices的用戶名令牌。它並不像你想象的那麼直截了當。你需要包含一個WSE頭。

對此的解決方案是Rick Strahl's blog.

public class CustomTokenSerializer : WSSecurityTokenSerializer { 
    public CustomTokenSerializer(SecurityVersion sv) 
     : base(sv) { } 

    protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) { 
     var userToken = token as UserNameSecurityToken; 
     var tokennamespace = "o"; 
     var nonce = GetSHA1String(Guid.NewGuid().ToString()); 
     writer.WriteRaw(
       [email protected]"<{tokennamespace}:UsernameToken u:Id=""{token.Id}"" xmlns:u=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\""> 
       <{tokennamespace}:Username>{userToken.UserName}</{tokennamespace}:Username> 
       <{tokennamespace}:Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\"">{userToken.Password}</{tokennamespace}:Password> 
       <{tokennamespace}:Nonce EncodingType=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\"">{nonce}</{tokennamespace}:Nonce> 
      </{tokennamespace}:UsernameToken>" 
      ); 
    } 

    protected string GetSHA1String(string phrase) { 
     SHA1CryptoServiceProvider sha1Hasher = new SHA1CryptoServiceProvider(); 
     byte[] hashedDataBytes = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(phrase)); 
     return Convert.ToBase64String(hashedDataBytes); 
    } 

} 

我使用的ChannelFactory來創建客戶端。

public class WebServiceClientFactory<T> : IWebServiceClientFactory<T> { 

    public WebServiceClientFactory() { 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 
     ServicePointManager.Expect100Continue = true; 
     ServicePointManager.DefaultConnectionLimit = 9999; 
    } 

    public T GetClient(Credentials cred) { 

     ChannelFactory<T> channelFactory = new ChannelFactory<T>(GetBinding(), new EndpointAddress(cred.Url)); 
     channelFactory.Endpoint.Behaviors.Remove<System.ServiceModel.Description.ClientCredentials>(); 
     channelFactory.Endpoint.Behaviors.Add(new CustomCredentials()); 
     channelFactory.Endpoint.Behaviors.Add(new CustomP6DbInstanceBehavior(cred.DatabaseInstanceId)); 

     channelFactory.Credentials.UserName.UserName = cred.Username; 
     channelFactory.Credentials.UserName.Password = cred.Password; 

     return channelFactory.CreateChannel(); 
    } 

    private Binding GetBinding() { 
     var security = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); 
     security.IncludeTimestamp = false; 
     var encoding = new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8); 
     var transport = new HttpsTransportBindingElement { 
      MaxReceivedMessageSize = 20000000 // 20 megs 
     }; 
     return new CustomBinding(security, encoding, transport); 
    } 

} 
相關問題