我创建了一个具有 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 = …Run Code Online (Sandbox Code Playgroud) wcf ×1