2015-05-08 338 views
4

我使用HttpContext檢索即將到來的HTTP請求的當前用戶名,但是在運行覆蓋率分析時,它會報告資源泄漏。獲取HttpContext.Current.User.Identity.Name時發生資源泄漏

public class UsersController:ApiController 
    { 
     private string userName; 
     public UsersController() 
     { 
     if (HttpContext.Current != null) 
     { 
      userName = HttpContext.Current.User.Identity.Name; 
     } 
    } 
    //I defined customized identity 
    public class MyIdentity : IIdentity 
    { 
     private string name; 
     public string AuthenticationType 
     { 
      get { return "Custom"; } 
     } 

     public bool IsAuthenticated 
     { 
      get { return true; } 

     } 

     public string Name { get; set; } 

} 

在Coverity的報告,它說 2. alloc_fn:一個新的資源是從分配方法Identity.get返回。 (虛擬呼叫解析爲System.Security.Claims.ClaimsPrincipal.Identity.get。) 3. noescape:資源System.Web.HttpContext.Current.User.Identity未關閉或保存在Name.get中。 (虛擬呼叫解析爲Org.Abc.HttpModules.MyIdentity.Name.get。)

CID 51307:資源泄漏(RESOURCE_LEAK) 4. leaked_resource:未能保存或通過System.Web.HttpContext創建密切資源。 Current.User.Identity泄漏它。

+1

您是否找到解決該問題的解決方案? –

+0

@AbdulAhadMonty我直接聯繫覆蓋支持,他們認爲它是誤報,所以沒有錯誤,我的執行:) – Bargitta

+0

謝謝@Bargitta –

回答

0

IIdentity由WindowsIdentity執行。 WindowsIdentity也實現了IDisposable,所以在創建後需要處理。您可以通過調用Dispose方法來完成此操作。

但是,由於您正在使用自己的IIdentity實現,因此我認爲這裏的問題是Coverity不確定該身份是否是一次性的,因此出於謹慎的考慮而犯錯。