当我尝试使用远程wcf服务创建新用户时,我遇到此错误,其他服务工作正常。 {“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; }
}
我的服务器模型配置
<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 :(得分:0)
由于您已在服务上启用了基本身份验证,因此您需要根据请求发送基本凭据(用户名/密码)。在致电您的服务之前,请尝试添加以下代码。
_proxy.ClientCredentials.UserName.UserName = "myuser";
_proxy.ClientCredentials.UserName.Password = "mypassword";