【问题标题】:WCF Authentication and ImpersonationWCF 身份验证和模拟
【发布时间】:2012-09-14 01:49:48
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    在 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

    【讨论】:

    • 这在技术上是正确的答案,但是据我所知,身份验证和角色服务不允许您装饰 OperationalBehaviours。
    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    相关资源
    最近更新 更多