wcf 求助 ,哪位大神帮帮忙

maomixi 2012-11-14 10:32:00
服务器代码,没写配置都是通过代码实现

string Url = (string)config.GetValue("ServerAddress", typeof(string));
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType =MessageCredentialType.Certificate;
ServiceMetadataBehavior met = new ServiceMetadataBehavior();
met = new ServiceMetadataBehavior() { HttpGetEnabled = true };

Uri httpUri = new Uri(Url);


ServiceHost myServiceHost = new ServiceHost(typeof(DataService), httpUri);
myServiceHost.AddServiceEndpoint(typeof(IDataService), binding, "");
myServiceHost.Description.Behaviors.Add(met);

myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "MyCert");
myServiceHost.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
myServiceHost.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustomValidator();
//host承载服务
try
{
myServiceHost.Open();
if (myServiceHost.State == CommunicationState.Opened)//判断服务状态
return true;
else
return false;
}
catch (Exception e)
{
return false;
}

客户端

DataServiceClient data = new DataServiceClient();
data.ClientCredentials.UserName.UserName = "xiao";
data.ClientCredentials.UserName.Password = "123456";

Label1.Text = data.PrintMessage("123");

错误:

无法满足对安全令牌的请求,因为身份验证失败。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ServiceModel.FaultException: 无法满足对安全令牌的请求,因为身份验证失败。

源错误:


行 334:
行 335: public string PrintMessage(string message) {
行 336: return base.Channel.PrintMessage(message);
行 337: }
行 338:

...全文
149 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
设置了身份验证、X509证书验证(你证书授权了没) 授权了就新建个类继承'System.IdentityModel.Selectors.UserNamePasswordValidator' 然后我们重写里面的'Validate'方法来实现用户名密码认证

public class UserNamePasswordValidator : System.IdentityModel.Selectors.UserNamePasswordValidator 
{ 
    public override void Validate(string userName, string password) 
    { 
        if (userName != "xiao" || password != "123456") 
        { 
            throw new System.IdentityModel.Tokens.SecurityTokenException("Unknown Username or Password"); 
        } 
    } 
} 

maomixi 2012-11-14
  • 打赏
  • 举报
回复
在线等丫,求助,顶上去

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧