2016-02-18 108 views
0

我有這個錯誤,當我嘗試使用遠程wcf服務創建新用戶時,其他服務正常工作。 {「HTTP請求被禁止,客戶端認證方案'Basic'。」}。HTTP請求被禁止,客戶端身份驗證方案'Basic'

我的用戶控制器

[AcceptVerbs("Post")] 
    public ActionResult Add(FormCollection collection) 
    { 
     _userRepository.Add(collection["Company"], collection["Email"], collection["ExternalIdentifier"]); 
     return RedirectToAction("LoggedInAsAdministrator", "Home"); 
    }  

我的用戶信息庫

public void Add(string company, string email, string eid) 
    { 
     var user = new User { Company = company, Email = email, ExternalIdentifier = eid, AccountType = "pro", CountryCode = "NL", Language="EN" }; 
     var createdUser = _proxy.CreateUser(user); 

     ((IDisposable)_proxy).Dispose(); 
    }  

我user.cs:

[ServiceContract] 
[XmlRoot("user")] 
public class User 
{ 
    [XmlElement("company")] 
    public string Company { get; set; } 

    [XmlElement("country-code")] 
    public string CountryCode { get; set; } 

    [XmlElement("language")] 
    public string Language { get; set; } 

    [XmlElement("created-at")] 
    public DateTime? CreatedAt { get; set; } 

    [XmlElement("email")] 
    public string Email { get; set; } 

    [XmlElement("external-identifier")] 
    public string ExternalIdentifier { get; set; } 

    [XmlElement("id")] 
    public int? Id { get; set; } 

    [XmlElement("measurement-system")] 
    public string MeasurmentSystem { get; set; } 

    [XmlElement("profile")] 
    public string Profile { get; set; } 

    [XmlElement("url")] 
    public string Url { get; set; } 

    [XmlElement("username")] 
    public string Username { get; set; } 

    [XmlElement("account-type")] 
    public string AccountType { get; set; } 

    [XmlElement("current-token")] 
    public string CurrentToken { get; set; } 



}  

我servermodel配置

<system.serviceModel> 
    <bindings> 
     <webHttpBinding> 
     <binding name="UsernameWithTransport"> 
      <security mode="Transport"> 
      <transport clientCredentialType="Basic" /> 
      </security> 
     </binding> 
     </webHttpBinding> 
    </bindings> 


    <client> 
     <endpoint address="xxxxxxx" binding="webHttpBinding" bindingConfiguration="UsernameWithTransport" behaviorConfiguration="xxxxx" contract="xxxxx" name="xxxxx" /> 
    </client> 


    <behaviors> 
     <endpointBehaviors> 
     <behavior name="xxxxxxxx"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 

    </behaviors> 
    </system.serviceModel> 

任何幫助,請。

回答

0

由於您已啓用基本身份驗證服務,因此您需要發送包含請求的基本憑據(用戶名/密碼)。在致電您的服務之前,嘗試添加以下代碼。

_proxy.ClientCredentials.UserName.UserName = "myuser"; 
_proxy.ClientCredentials.UserName.Password = "mypassword"; 
+0

我打電話的工廠實例的用戶名和密碼時,我實例化我的用戶資源庫,這不是probleme – dardouri90

+0

你能後的代碼,您是如何設置的服務代理憑據? –

+0

這就是我所說的渠道工廠: private readonly ChannelFactory _factory; private readonly IxxxApi _proxy; public UserRepository() { _factory = xxxFactory.GetxxxCatory(); _proxy = _factory.CreateChannel(); } – dardouri90

相關問題