WCF身份验证和模拟

时间:2012-09-14 01:49:48

标签: asp.net wcf wcf-security wcf-client

嗨,我在实现WCF RoleService时遇到了一些问题,特别是GetAllRolesForCurrentUser方法。我可以成功连接到服务,但是当我尝试为用户检索角色时,它自然地使用当前主体标识(即运行服务的用户)。但是,我需要已登录的用户。

我知道我必须传递角色服务自定义凭据(用户名/密码),但是如何让服务模仿该用户。

1 个答案:

答案 0 :(得分:0)

在WCF服务中实现模拟

1)用OperationBehavior装饰操作并给出“Impersonation = ImpersonationOption.Required”,如下面的代码所示

[ServiceContract]
public interface IHelloContract
{
    [OperationContract]
    string Hello(string message);
}

public class HelloService : IHelloService
{
    [OperationBehavior(Impersonation = ImpersonationOption.Required)]
    public string Hello(string message)
    {
        return "hello";
    }
}

2)客户端将其称为

  using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
    {
        HelloService.ServiceClient myService = new HelloService.ServiceClient();
        Console.WriteLine(myService.Hello("How are you?"));
        myService.Close();
    }

关注链接以获取进一步参考:http://msdn.microsoft.com/en-us/library/ff650591.aspx#_Step_7:_Impersonate