Asp.net WCF身份验证和模拟

Asp.net WCF身份验证和模拟,asp.net,wcf,wcf-security,wcf-client,Asp.net,Wcf,Wcf Security,Wcf Client,​您好,我在实现WCF角色服务时遇到了一些问题,特别是GetAllRolesForCurrentUser方法。我可以成功连接到服务,但当我尝试检索用户的角色时,它自然会使用当前主体标识(即运行服务的用户)。但是,我需要它的用户谁已经登录 我知道我必须传递角色服务自定义凭据(用户名/密码),但如何让服务模拟该用户。在WCF服务中实现模拟 1) 用OperationBehavior修饰操作并给出“Impersonation=ImpersonationOption.Required”,如下代码所示 [

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


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

在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();
    }

按照链接进一步参考:

从技术上讲,这是正确的答案,但是,据我所知,身份验证和角色服务不允许您修饰操作行为。