2012-07-04 37 views
1

我使用ASP.NET成員資格。如果我所關心的是我是否有非空的Membership.GetUser(),是否有理由使用Request.IsAuthenticated?Request.IsAuthenticated VS Membership.GetUser()

在開發過程中,我無意中創建了Request.IsAuthenticated爲true,但Membership.GetUser()爲null的情況,所以我會通過[Authorize]過濾器測試(全局應用,特別應用[AllowAnonymous]根據需要),但以後失敗。雖然這種情況在生產中可能不會發生,但我仍想對此進行解釋。

鑑於此,編寫一個自定義過濾器,比如說AuthorizeMembership或者其他一些對Membership.GetUser()而不是Request.IsAuthenticated進行檢查的自定義過濾器是否合適?任何需要注意的問題?

如果是這樣,將它也沒關係使用過濾器來填充我通常需要處理請求的用戶屬性全局的UserInfo對象?我根本不使用成員資格配置文件,而是在單獨的應用程序數據庫中獨立管理我的用戶屬性。

+0

您可能需要詳細說明這些「情況」。代碼示例也會有所幫助。因爲它似乎是你的代碼來創建這樣的無用的情況,而不是避免它們,你試圖找出一個更糟的解決方法。 –

+0

@Wiktor,這些情況不會在我的代碼中創建。如果用戶已登錄,然後從成員資格中外部刪除,則Request.IsAuthenticated將爲true,但GetUser()將爲空。是否需要代碼才能回答第一個問題?不知道我應該提供那裏,因爲我問我應該寫什麼(如果我想授權爲GetUser()爲中心而不是IsAuthenticated爲中心),而不是我寫的。 – osoviejo

+0

從你的寫作我明白,只有噹噹前用戶從數據庫中刪除,然後在同一個請求中完成對'IsAuthenticated'的調用時纔會出現問題。如果是這樣,爲什麼用戶在從數據庫中刪除後沒有立即註銷? –

回答

0

主要不同是速度。

Request.IsAuthenticated更快並且使用內部緩存標誌,比Membership.GetUser()

明白爲什麼一個比其他快我放在這裏的代碼。

public virtual bool IsAuthenticated 
{ 
    get 
    { 
     if (this.m_isAuthenticated == -1) 
     { 
      WindowsPrincipal principal = new WindowsPrincipal(this); 
      SecurityIdentifier sid = new SecurityIdentifier(IdentifierAuthority.NTAuthority, new int[] { 11 }); 
      this.m_isAuthenticated = principal.IsInRole(sid) ? 1 : 0; 
     } 
     return (this.m_isAuthenticated == 1); 
    } 
} 

GetUser()有太多的電話,因爲實際上需要更多的信息,是給予。

public static MembershipUser GetUser() 
{ 
    return GetUser(GetCurrentUserName(), true); 
} 

private static string GetCurrentUserName() 
{ 
    if (HostingEnvironment.IsHosted) 
    { 
     HttpContext current = HttpContext.Current; 
     if (current != null) 
     { 
      return current.User.Identity.Name; 
     } 
    } 
    IPrincipal currentPrincipal = Thread.CurrentPrincipal; 
    if ((currentPrincipal != null) && (currentPrincipal.Identity != null)) 
    { 
     return currentPrincipal.Identity.Name; 
    } 
    return string.Empty; 
} 

public static MembershipUser GetUser(string username, bool userIsOnline) 
{ 
    SecUtility.CheckParameter(ref username, true, false, true, 0, "username"); 
    return Provider.GetUser(username, userIsOnline); 
} 

internal static void CheckParameter(ref string param, bool checkForNull, bool checkIfEmpty, bool checkForCommas, int maxSize, string paramName) 
{ 
    if (param == null) 
    { 
     if (checkForNull) 
     { 
      throw new ArgumentNullException(paramName); 
     } 
    } 
    else 
    { 
     param = param.Trim(); 
     if (checkIfEmpty && (param.Length < 1)) 
     { 
      throw new ArgumentException(SR.GetString("Parameter_can_not_be_empty", new object[] { paramName }), paramName); 
     } 
     if ((maxSize > 0) && (param.Length > maxSize)) 
     { 
      throw new ArgumentException(SR.GetString("Parameter_too_long", new object[] { paramName, maxSize.ToString(CultureInfo.InvariantCulture) }), paramName); 
     } 
     if (checkForCommas && param.Contains(",")) 
     { 
      throw new ArgumentException(SR.GetString("Parameter_can_not_contain_comma", new object[] { paramName }), paramName); 
     } 
    } 
} 
+0

感謝Aristos。如此以來,IsAuthenticated便宜和的getUser()則不然,它是值得永遠門的getUser()與IsAuthenticated檢查,避免的getUser()工作,如果它只是將返回null,正確嗎?如果我在授權過濾器中實現這個功能,我可以獲取我感興趣的所有用戶信息(如果GetUser()非空),並將對象實例暴露給控制器? – osoviejo

相關問題