WCF:从另一方收到不安全或错误安全的故障

Bri*_*hah 5 wcf

我创建了一个具有 aspnet 角色和安全性的 wcf 服务。

当我在一个网站上使用它并尝试检查它时,使用正确的用户名和密码它可以正常工作,但是使用不正确的用户名和密码它会给我一个错误:

从另一方收到不安全或不正确保护的故障。有关故障代码和详细信息,请参阅内部 FaultException。

我本来希望得到“访问被拒绝”。

所以,知道为什么吗???

更新

你是对的,我们可以添加 FaultContracts ......但即使我添加了它也会给我同样的错误......此外,我试图检查同一个用户是否是 inrole ,如果不是,然后从外部触发了 FaultException ......

仍然得到相同的错误,没有得到实际的“访问被拒绝”。

只是为了更新您,我已使用 aspnet 表单身份验证来检查凭证和角色......代码有点像

public class SampleService : ISampleService
{
    [PrincipalPermission(SecurityAction.Demand, Role = "admin")]
    public string GetCurrentTime()
    {
        if (!Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "admin"))
            return DateTime.Now.ToString();
        else
            throw new FaultException("Access is denied");
    }
}

[ServiceContract]
public interface ISampleService
{
    [OperationContract]
    string GetCurrentTime();
}
Run Code Online (Sandbox Code Playgroud)

在客户端,我消费的地方,这项服务......

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            ChannelFactory<AuthCustWithAspnetRoleService.ISampleService> factory = new ChannelFactory<AuthCustWithAspnetRoleService.ISampleService>("*");
            factory.Credentials.UserName.UserName = txtUserName.Text.Trim(); // "admin"; 
            factory.Credentials.UserName.Password = txtPassword.Text.Trim(); // "admin";
            AuthCustWithAspnetRoleService.ISampleService proxy = factory.CreateChannel();
            lblMessage.Text = proxy.GetCurrentTime();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望这将指导您更多地了解我的代码,并且您可以更详细地帮助我......我不知道为什么其他人(我理解这个概念的参考很少)得到“访问被拒绝”而我没有直接得到这个从系统...

Ger*_*nck 1

您应该使用FaultContracts来避免出现此消息。