2008-11-15 36 views
20

對於Web服務和WCF我很綠,而且我正在使用Windows集成身份驗證 - 如何在服務器端界面上獲取用戶名?我相信我應該實現一個自定義的行爲,或者可能是WCF會話的東西?任何線索都會超級方便。從WCF服務器端獲取Windows用戶名

回答

9

這裏的服務代碼片段展示瞭如何獲取並使用與WCF服務的調用相關的的WindowsIdentity。

此代碼假設您正在接受配置的大部分默認值。它應該在命名管道或網絡TCP綁定中沒有任何問題。

p.Demand()將確定用戶是否位於由permissionGroup變量指定的窗口組中。

private static void DemandManagerPermission() 
{ 
    // Verify the use has authority to proceed 
    string permissionGroup = ConfigurationManager.AppSettings["ManagerPermissionGroup"]; 
    if (string.IsNullOrEmpty(permissionGroup)) 
     throw new FaultException("Group permissions not set for access control."); 

    AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); 
    var p = new PrincipalPermission(ServiceSecurityContext.Current.WindowsIdentity.Name, permissionGroup, true); 
    p.Demand(); 

} 
+1

p.Demand將要求「Thread.CurrentPrincipal」處於指定角色AND具有與「ServiceSecurityContext.Current」相同的用戶名.WindowsIdentity.Name`。 – Joe 2015-11-13 14:22:27

-2

你試過WindowsIdentity.GetCurrent();

+8

這給出了服務正在運行的標識 - 而不是用戶調用服務的標識 – Cocowalla 2011-02-24 11:16:04

49

嘗試尋找在ServiceSecurityContext.Current.WindowsIdentity

5

要獲得WCF服務調用者的用戶名:

VAR callerUserName = ServiceSecurityContext.Current.WindowsIdentity.Name;

相關問題