2011-12-13 41 views
1

我有一個使用Windows身份驗證的WCF服務的Silverlight應用程序。 我的用戶獲得了正確的身份驗證。我可以在客戶端訪問他們的用戶名和角色。 但是我如何在服務器端做同樣的事情呢? 我已經試過這如何在WCF服務上使用Windows身份驗證訪問服務器端的用戶名?

ServiceSecurityContext.Current.WindowsIdentity 

IIdentity caller = ServiceSecurityContext.Current.PrimaryIdentity; 

這是我發現的在這裏計算器類似的問題的答案,但它不編譯。 我得到'ServiceSecurityContext不包含當前'定義的異常。

那麼如何讓服務器端的用戶?

回答

1

嘗試

HttpContext.Current.User; 

希望這有助於

+1

如果該服務已被設置爲ASP.NET兼容模式這隻會工作。 – syneptody

+0

真的嗎?這不是從請求頭獲取用戶名嗎? –

+0

根據:http://msdn.microsoft.com/en-us/library/aa702682.aspx「HttpContext:當從WCF服務中訪問時,Current始終爲空,請改用RequestContext。」 ...除非您的服務使用ASP.NET兼容 – syneptody

2
public static string GetUserIdentity() 
{ 
    return OperationContext.Current.ServiceSecurityContext.WindowsIdentity.Name; 
} 

OperationContext位於System.ServiceModel命名空間中。

+1

嗯,我想這一點,但我的ServiceSecurityContext爲空.. – grabah

相關問題