我创建了一项具有 aspnet 角色和安全性的 WCF 服务。
当我在一个网站上使用它并尝试检查是否使用正确的用户名和密码时,它工作得很好,但是如果用户名和密码不正确,它会给我一个错误:
从对方收到不安全或不正确安全的故障
派对。有关错误代码和详细信息,请参阅内部FaultException。
我本来希望得到“访问被拒绝”。
那么,知道为什么吗???
UPDATE
你是对的,我们可以添加FaultContracts...但即使我添加了它也会给我同样的错误..此外,我尝试检查同一用户是否为角色,如果没有,则从外部触发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();
}
在客户端,我消费的地方,该服务......
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;
}
}
希望这将指导您更多地了解我的代码,并且您可以更详细地帮助我......
我不知道为什么其他人(我理解这个概念的地方很少参考)得到“访问被拒绝”,而我没有直接从系统得到这个......