0

I have this bug when i try to create a new user using a remote wcf service, other services work fine. {"The HTTP request was forbidden with client authentication scheme 'Basic'."}.

My user controller

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

my user repository

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();
    }    

my 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; }



}    

my servermodel configuration

<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>

Any Help PLEASE.

uselpa
  • 18,732
  • 2
  • 34
  • 52

1 Answers1

0

Since you have enabled basic authentication on service you need to send basic credentials (username/password) with request. Try adding following code before you call your service.

_proxy.ClientCredentials.UserName.UserName = "myuser";
_proxy.ClientCredentials.UserName.Password = "mypassword";
Pankaj Kapare
  • 7,486
  • 5
  • 40
  • 56
  • i'm calling a factory that instantiate the username and the password when i instantiate my user repository so that's not the probleme – dardouri90 Feb 18 '16 at 18:58
  • Can you post code how you are setting credentials on service proxy? – Pankaj Kapare Feb 18 '16 at 19:12
  • this is how i call the channel factory: private readonly ChannelFactory _factory; private readonly IxxxApi _proxy; public UserRepository() { _factory = xxxFactory.GetxxxCatory(); _proxy = _factory.CreateChannel(); } – dardouri90 Feb 22 '16 at 17:33
  • and this is my factory: public static ChannelFactory GetxxxCatory() { var factory = new ChannelFactory("xxxREST"); factory.Credentials.UserName.UserName = ConfigurationManager.AppSettings["APIKey"]; factory.Credentials.UserName.Password = ConfigurationManager.AppSettings["Password"]; return factory; } – dardouri90 Feb 22 '16 at 17:36