2012-09-14 30 views
0

嗨,我在實現WCF RoleService,特別是GetAllRolesForCurrentUser方法時遇到了一些麻煩。我可以成功連接到服務,但是當我嘗試並檢索用戶的角色時,它自然使用當前主體標識(即服務正在運行的用戶)。但是,我需要它爲登錄用戶。WCF身份驗證和冒充

我知道我必須通過角色服務自定義憑據(用戶名/密碼),但你如何獲得服務來模擬該用戶。

回答

0

爲了實現模擬在WCF服務

1)與裝飾OperationBehavior的操作和得到的 「冒充= 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

+0

這在技術上是正確的答案,然而,身份驗證和角色服務不允許你裝飾OperationalBehaviours據我所知。 – Dan